我有一个这样的查询:
SELECT * FROM `purchases` p
JOIN `purchase_types` pt ON p.purchase_type = pt.node
当我在 PHPmyAdmin 中运行它时,它会返回正确的结果集,如下所示:
node | purchase_type | amount_spent | node | name
--------------------------------------------------
2 | 5 | 8.5000 | 5 | Lunch
3 | 5 | 1.5000 | 5 | Lunch
4 | 6 | 4.6600 | 6 | Dinner
这是我的 PHP 代码:
$sql = "SELECT * FROM `purchases` p
JOIN `purchase_types` pt ON p.purchase_type = pt.node";
$query = mysql_query($sql);
$result = mysql_fetch_assoc($query);
$purchases = array();
while($row = mysql_fetch_assoc($query)) {
$purchases[] = array(
'name' => $row['name'],
'amount_spent' => $row['amount_spent']
);
}
for eaching over$expenses
返回以下输出:
3 | 5 | 1.5000 | 5 | Lunch
4 | 6 | 4.6600 | 6 | Dinner
第一次“午餐”会发生什么?如何让 PHP 输出与直接 MySQL 查询输出相同?