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);
    }

    // ...
}

Leave a Reply

*

Hire Me
Follow Me!
Search
Most Popular Articles & Pages
Because your vote is Important
Sorry, there are no polls available at the moment.
Categories