PHP MySQL:返回Query作为数组索引名称和值


PHP-MySQL: Returning Query as Array index name and value

我有一个MySQL表,如下所示:

------------
|animal|legs|
------------
| cat     4 |
| chicken 2 |

是否可以在PHP中返回一个以动物为索引名称、以腿为值的数组,而不必像在mysqli_fetch_all中那样逐个获取?

$result[cat]=4
$result[chicken]=2.

我试过

$q = mysqli_fetch_all($q);
$array=array_combine($q2[0], $q2[1]);

但它并没有给我想要的输出。

使用safeMysql

$dic = $db->getIndCol("animal","SELECT * FROM animals");

尝试:

$result = array();
while ($row = mysqli_fetch_all($q, MYSQLI_ASSOC)){
   $result[$row['animal']] = $row['legs'];
}

记住用这种方式在数组标签中的字符串周围使用撇号,如"cat"或"chicken"。