Php 函数只返回数组的第一个元素

Php 函数只返回数组的第一个元素,php,mysql,Php,Mysql,应该很简单,但我就是想不出来。我有一个函数,它返回一个数组,从MySql数据库中获取。返回的数组应该有2个元素 以下是函数: function path_to_photo ($id) { $query = mysql_query("Select path from photos1 where listingsdb_id = $id"); $test_array = mysql_fetch_array($query); return $test_array; } $one=

应该很简单,但我就是想不出来。我有一个函数,它返回一个数组,从MySql数据库中获取。返回的数组应该有2个元素

以下是函数:

function path_to_photo ($id)
{
   $query = mysql_query("Select path from photos1 where listingsdb_id = $id"); 
   $test_array = mysql_fetch_array($query);

   return $test_array;
}

$one=2;
$test = path_to_photo($one); 
echo $test[0]; // --> this works fine
echo $test[1]; //-->this gives an error - undefined offset etc.
起初我以为我的数组实际上只返回一个元素,但当我把

$query = mysql_query("Select path from photos1 where listingsdb_id=$id"); 
$test_array = mysql_fetch_array($query);

return sizeof($test_array);
它给了我正确的数字2。我只是不明白第二个索引会发生什么。以下是MySQL中的查询输出

mysql> select path from photos1 where listingsdb_id=2;
+------------------+
| path             |
+------------------+
| small_photo.jpg  |
| big_photo.jpg |
+------------------+

mysql\u fetch\u array
返回一个数组,该数组包含结果中下一行的数据

您说过
选择路径
,因此结果中只有一列,因此数组长度应为
1


如果你想得到一个包含所有
路径的数组,那么你必须创建一个空数组,然后反复调用
mysql\u fetch\u array
,将每个条目依次放入前面提到的数组中,然后返回该数组。

危险:你正在使用并且应该使用。您还容易受到现代API的攻击,因为它会使您更容易使用。
mysql\u fetch\u array
需要循环