php - How to get Mysqli Stmt to fetch multiple rows correctly? -
php - How to get Mysqli Stmt to fetch multiple rows correctly? -
i've been reaching end of ideas crazy problem.
using mysqli run next query
select * `shop_cart` `tmpid`=?
it should homecoming each row in array this:
array ( [0] => array ( [id] => 1 [tmpid] => af83abab7fdee8eb0cf8919f171cdeec [pid] => 800 [qty] => 2 [time] => 1310076898 ) [1] => array ( [id] => 2 [tmpid] => af83abab7fdee8eb0cf8919f171cdeec [pid] => 797 [qty] => 1 [time] => 1310076903 ) [2] => array ( [id] => 3 [tmpid] => af83abab7fdee8eb0cf8919f171cdeec [pid] => 883 [qty] => 1 [time] => 1310076907 ) [3] => array ( [id] => 4 [tmpid] => af83abab7fdee8eb0cf8919f171cdeec [pid] => 795 [qty] => 1 [time] => 1310076909 ) )
instead 4 copies of same row this:
array ( [0] => array ( [id] => 4 [tmpid] => af83abab7fdee8eb0cf8919f171cdeec [pid] => 795 [qty] => 1 [time] => 1310076909 ) [1] => array ( [id] => 4 [tmpid] => af83abab7fdee8eb0cf8919f171cdeec [pid] => 795 [qty] => 1 [time] => 1310076909 ) [2] => array ( [id] => 4 [tmpid] => af83abab7fdee8eb0cf8919f171cdeec [pid] => 795 [qty] => 1 [time] => 1310076909 ) [3] => array ( [id] => 4 [tmpid] => af83abab7fdee8eb0cf8919f171cdeec [pid] => 795 [qty] => 1 [time] => 1310076909 ) )
the problem lies somewhere in bit of code think:
while ($query->fetch()){ $results[] = $fields; }
if set print_r($fields)
in this:
while ($query->fetch()){ print_r($fields); $results[] = $fields; }
it prints each row of results correctly. if set print_r($results)
in here this:
while ($query->fetch()){ $results[] = $fields; } print_r($results);
...then 1 big array containing duplicate copes of 1 row. seems me $results array isn't getting populated correctly. seems info coming out of database okay.
any help great, i'm @ end of rope @ figuring out!
edit
here's more code preceding (and including) fetch() loop posted above.
// generate types $types = ''; //initial sting types foreach($params $param) { //for each element, determine type , add together if(is_int($param)) { $types .= 'i'; //integer } elseif (is_float($param)) { $types .= 'd'; //double } elseif (is_string($param)) { $types .= 's'; //string } else { $types .= 'b'; //blob , unknown } } array_unshift($params, $types); $query = $this->connection->stmt_init(); if($query->prepare($sql)) { call_user_func_array(array($query,'bind_param'),$this->refvalues($params)); $query->execute(); if($fetch){ // if want homecoming array of results // metadata $meta = $query->result_metadata(); $fields = $results = array(); while ($field = $meta->fetch_field()) { $var = $field->name; $$var = null; $fields[$var] = &$$var; } call_user_func_array(array($query,'bind_result'),$fields); while ($query->fetch()){ pr($fields); $results[] = $fields; } } } else die(printf("error: %s\n", $this->connection->error.' : '.$this->lastq));
most you've declared $fields
reference somewhere in code (explicitly or implicitly). each array element append $results
going re-create of same reference, each array element points same bit of memory in php, which'll happen lastly row fetched query.
i'd suggest doing unset($fields)
before variable binding/result fetching, break reference setting on $fields
variable.
php mysqli prepared-statement
Comments
Post a Comment