Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在查询后从数组中获取正确的数据?_Php_Mysql_Database - Fatal编程技术网

Php 在查询后从数组中获取正确的数据?

Php 在查询后从数组中获取正确的数据?,php,mysql,database,Php,Mysql,Database,我有一个sql语句 "SELECT o.orderID, p.productID, p.name, o.qty, p.mouldID FROM products AS p INNER JOIN orderedProducts AS o ON o.productID = p.productID WHERE o.orderID = '$id'"; 当我在phpmyadmin中使用它来测试它时,它看起来是这样的(我使用ID56) 但吐出的数组是: Array ( [0] => 56 [orde

我有一个sql语句

"SELECT o.orderID, p.productID, p.name, o.qty, p.mouldID FROM products AS p INNER JOIN orderedProducts AS o ON o.productID = p.productID WHERE o.orderID = '$id'";
当我在phpmyadmin中使用它来测试它时,它看起来是这样的(我使用ID56)

但吐出的数组是:

Array ( [0] => 56 [orderID] => 56 [1] => 11 [productID] => 11 [2] => newproduct [name] => newproduct [3] => 2000 [qty] => 2000 [4] => 4 [mouldID] => 4 )
这不是我想要的数据,所以我试着看看如果我把它放到一个表中会发生什么

它回来了

0         | 56
orderID   | 56
1         |  11
productID |  11
2         | newproduct
name      | newproduct
3         |  2000
qty       |  2000
4         |  4
mouldID   |  4
显然出了点问题


我使用
mysql\u fetch\u array
将数据从数据库中获取到数组中……这可能是我做错的吗

基本上,我希望它看起来像数据库返回的第一个表

任何想法…谢谢

mysql\u fetch\u XXX()函数只返回下一行,而不是所有结果。如果需要一个包含所有行的数组,则必须使用循环来构造它:

while ($row = mysql_fetch_assoc($query)) {
  $results[] = $row;
}
var_dump($results);
mysql\u fetch\u array()
一次只返回一行。你必须在循环中调用它来获取所有数据。
while ($row = mysql_fetch_assoc($query)) {
  $results[] = $row;
}
var_dump($results);