Strange PHP array behavior overwriting values with all the same values

Posted by dasdas on Stack Overflow See other posts from Stack Overflow or by dasdas
Published on 2010-04-15T21:38:08Z Indexed on 2010/04/15 21:43 UTC
Read the original article Hit count: 122

Filed under:
|

Im doing a simple mysqli query with code ive used many times before but have never had this problem happen to me. I am grabbing an entire table with an unknown number of columns (it changes often so i dont know the exact value, nor the column names). I have some code that uses metadata to grab everything and stick it in an array.

This all works fine, but the output is messed up:

$stmt -> execute();  //the query is legit, no problems there
$meta = $stmt->result_metadata();
while ($field = $meta->fetch_field())
{
    $params[] = &$row[$field->name];
}
call_user_func_array(array($stmt, 'bind_result'), $params);

while ($stmt->fetch())
{
    $pvalues[++$i] = $row; //$pvalues is an array of arrays. row is an array
    //print_r($row);
    print_r($pvalues[$i-1]);
}
$stmt -> close();

I would assume that $pvalues has the results that I am looking for. My table currently has 2 rows. $pvalues has array length 2. Both rows in $pvalues are exactly the same. If i use the:

print_r($row)

it prints out the correct values for both rows, but if later on i check what is in $pvalues it is incorrect (1 row is assigned to both indices of $pvalues).

If i use the

print_r($pvalues[$i-1])

it prints exactly as I expect, the same row in the table twice.

Why isnt the data getting assigned to $pvalues? I know $row holds the right information at one point, but it is getting overwritten or lost.

© Stack Overflow or respective owner

Related posts about php

Related posts about mysqli