Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extending mysqli_stmt to use a custom mysqli_result

I've been looking for an answer to this for quite a while now, but it seems nobody ever got around this problem before. Maybe some of you are able and willing to help me out on this... that would be great!

Im currently working on a mysqli wrapper and trying to implement a custom result class for prepared statements like i do for standard queries already! It seems the result gets generated in the stmt's "execute" method but i still fail to understand what's going on behind the scenes!

Is there a way (or hack) to point the generated results to my result class instead of the plain mysqli_result like its done with regular queries?

Just to get you an idea, here's a little paste from the code :

class extended_mysqli extends mysqli
{
    public function __construct()
    {
        call_user_func_array(array(get_parent_class($this), 'mysqli'), func_get_args());

        if ( $this->connect_errno )
        {
            throw new extended_mysqli_exception('database connection failure');
        }
    }

    public function query ($query, $binds = array())
    {
        if ( empty( $binds ) )
        {
            if ( $this->real_query($query) )
            {
                if ( $this->field_count )
                {
                    return new extended_mysqli_result($this, $query); // select, show, describe
                }
                else return true; // insert, update, delete
            }
            else return false; // fix 
        }
        else
        {
            $stmt = $this->prepare($query);

            if ( $stmt->bind_array($binds) )
            {
                return $stmt->execute() ? $stmt->get_result() : false;
            }
            else return false; 
        }
    }

    public function prepare($query)
    {
        return new extended_mysqli_stmt($this, $query);
    }

    // ...
}

class extended_mysqli_stmt extends mysqli_stmt
{
    public function __construct($link, $query)
    {
        parent::__construct($link);

        $this->prepare($query);
    }

    public function execute()
    {
        //  what do i do here ???
    }
}

class extended_mysqli_result extends mysqli_result implements countable, iterator, arrayaccess
{
    public function __construct($link, $mode = MYSQLI_STORE_RESULT)
    {
        parent::__construct($this->link = $link, $mode);
    }

    // ...
}
like image 435
Ivo Avatar asked Nov 18 '12 05:11

Ivo


1 Answers

Those classes were ment to be used as it is, so if you want your own implementation for mysqli you should write your own wrapper using mysqli_* functions.

mysqli Objects can be extended but this approach will ultimately fail if you wish to do any change to its structure or hierarchy.

Also, most of this methods trust your input data, so if you are trying to maintain a secure connection why not trying one of the 999 database abstraction layers out there and extending that code so it fits your needs?

like image 121
jtavares Avatar answered Nov 08 '22 10:11

jtavares