0

I have a MySQL table like this:

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

Is it possible to return an array in PHP with animals as its index name and legs as its values without fetching one by one as in mysqli_fetch_all ?

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

I tried

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

but it does not give me the output that I wanted.

4

2 回答 2

0

使用safeMysql

$dic = $db->getIndCol("animal","SELECT * FROM animals");
于 2013-07-29T16:03:14.263 回答
0

尝试:

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

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

于 2013-07-29T16:08:30.917 回答