Hello,

I am having trouble figuring out how to properly bind the results of a mysqli 
prepared statement using call_user_func_array.

I have an "AbstractModel" abstract class and within the class is a method 
called "load" that takes the primary key of the desired item and retrieves and 
loads the data from the database into the object.

abstract class AbstractModel {
        // lots of stuff omitted

        // this is just a rough draft so no error checking is implemented
        // $db is a subclass of mysqli
        // $primaryKeyAsTypedItem is an object that holds a type (int, float, 
string, etc.) and value
        public function load($db, $primaryKeyAsTypedItem) {
                $query = "SELECT * FROM " . $this->tableName . " WHERE " . 
$this->primaryKeyName . "=?"; 
                $ps = $db->prepare($query); 
                $type_string = $primaryKeyAsTypedItem->getTypeAbbreviation(); 
                $value = $db->escapeSql($primaryKeyAsTypedItem->getValue()); 
                $ps->bind_param($type_string, $value); 
                $ps->execute(); 
                $ps->store_result(); 
                $metadata = $ps->result_metadata()->fetch_fields(); 
                $params = array(); 
                foreach ($metadata as $object) { 
                        $params[$object->orgname] = null;
                } 
                call_user_func_array(array($ps, 'bind_result'), $params); 
                $ps->fetch(); 

                // see what is going on
                print "<pre>"; 
                var_dump($params); 
                print "</pre>";

                // more stuff omitted
        }
}

The problem I am having is that mysqli_stmt::bind_result expects a list of 
individual variables to which the results are bound, not just an array with the 
proper number of available indices.  I have done a lot of Googling and tried 
following the advice I found at [1], [2], [3], etc. but I was not successful in 
getting this to work.  I don't know how I can generically "expand" $params so 
that this will work for different tables with different fields.  Any pointers 
greatly appreciated.

[1] 
http://forums.devshed.com/php-development-5/mysqli-bind-result-to-return-array-568982.html
[2] 
http://www.charles-reace.com/blog/2009/04/28/mysqli-avoiding-bind_result-for-every-column/
[3] http://us.php.net/manual/en/mysqli-stmt.bind-result.php#102179
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to