【发布时间】:2014-09-16 04:12:20
【问题描述】:
我有这个选择查询,客户表中所有在交易表中有记录的记录都返回正常,但它不会从客户表中返回所有新添加的记录,在交易表上没有记录但只有 1 条记录。
我已经在 PHPMyAdmin 中尝试了在 SQL 中执行的语句,它非常好,返回所有记录。
脚本有什么问题?为什么它没有返回客户表中的所有记录,而交易表中没有记录但只有1条记录。
$q ="SELECT cust.*,
COALESCE(SUM(p.transactions_amount), 0) AS total
FROM customers cust
LEFT JOIN transactions p
ON p.transactions_customer_id = cust.customer_id
GROUP BY cust.customer_id
ORDER BY cust.customer_id DESC";
$query = mysqli_prepare($connection,$q);
if(mysqli_stmt_execute($query))
{
// execute query only
if($return === false)
{
mysqli_close($connection);
return true;
}
//return values
else
{
$result = array();
$meta = $query->result_metadata();
while ($field = $meta->fetch_field())
{
$params[] = &$row[$field->name];
}
call_user_func_array(array($query, 'bind_result'), $params);
while ($query->fetch())
{
var_dump($row);
$temp = array();
foreach($row as $key => $val)
{
$temp[$key] = $val;
}
$result[] = $temp;
}
$meta->free();
$query->close();
return $result;
}
}
else
{
mysqli_close($connection);
return false;
}
【问题讨论】:
-
尝试最基本的fetching 并调试,将其与您准备好的语句提取进行比较并找出