PHP function to handle most database queries has a problem with results. I am getting the right numb
Posted
by asdasds
on Stack Overflow
See other posts from Stack Overflow
or by asdasds
Published on 2010-06-16T19:55:00Z
Indexed on
2010/06/16
20:22 UTC
Read the original article
Hit count: 131
Here is my little function. It does not handle the results correctly. I do get all the rows that I want, but all the rows of the $results array contain the exact same values.
So i make 2 arrays, a temporary array to hold the values after each fetch, and another array to hold all the temporary arrays.
First i take the temp array and map its keys to the column names. Then i give it to bind_result, and call fetch() and use it like I would any other result value.
Could this be because I re-use the $results array?
numresults is the number of values you are taking from each row. if 0, you are not getting any results back.
function db_query($db, $query, $params = NULL, $numresults = 0)
{
if($stmt = $db -> prepare($query))
{
if($params != NULL)
{
call_user_func_array(array($stmt, 'bind_param'), $params);
}
if(!$stmt -> execute())
{
//echo 'exec error:',$db->error;
return false;
}
if($numresults > 0)
{
$results = array();
$tmpresult = array();
$meta = $stmt->result_metadata();
while ($columnName = $meta->fetch_field())
$tmpresult[] = &$results[$columnName->name];
call_user_func_array(array($stmt, 'bind_result'), $tmpresult);
$meta->close();
$results = array();
while($stmt -> fetch())
$results[] = $tmpresult;
}
$stmt -> close();
}
else
{
//echo 'prepare error: ',$db->error;
return false;
}
if($numresults == 0)
return true;
return $results;
}
© Stack Overflow or respective owner