-----问题解决了,请看下面的更新2 ---- 我把所有MySQL查询结果都放到了一个2D数组中:
$suppDescription=mysql_query($query);
$rows = mysql_num_rows($results);
$allSupplierInfo=array();
for($i=0; $i<$rows; $i++){
$allSupplierInfo[]=mysql_fetch_row($suppDescription);
}
但现在我无法访问$ allSuppliersInfo字段。
echo $allSupplierInfo[1][1]; // prints out 'Array'
echo $allSupplierInfo[1]['id']; //prints out nothing
我做错了什么?
-------更新-----
print_r($ allSupplierInfo)打印以下内容,因此看起来循环无法正常工作:
Array (
[0] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) )
[1] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) )
[2] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) [2] => Array ( [0] =>ID_C[1] => Name_C [2] => Address_C [3] => Link_C ) )
[3] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) [2] => Array ( [0] =>ID_C[1] => Name_C [2] => Address_C [3] => Link_C ) [3] => Array ( [0] =>ID_D[1] => Name_D [2] => Address_D [3] => Link_D ) )
[4] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) [2] => Array ( [0] =>ID_C[1] => Name_C [2] => Address_C [3] => Link_C ) [3] => Array ( [0] =>ID_D[1] => Name_D [2] => Address_D [3] => Link_D ) [4] => Array ( [0] =>ID_E[1] => Name_E [2] => Address_E [3] => Address_E ) ) )
------更新2 ----- 正如RiggsFolly所建议的,使用while循环解决了问题,我可以按照我最初的需要访问这些字段。我仍然不明白为什么我使用的for循环没有像我想的那样循环 - 任何解释都会非常感激。
答案 0 :(得分:1)
我认为你对你的mysql结果处理感到困惑,你在检查返回的行数时使用$result
但它应该是$suppDescription
。
这意味着您的for循环将无法运行,因为您将获得0或probbaly FALSE
作为对mysql_num_rows($suppDescription);
的回复
$suppDescription=mysql_query($query);
$rows = mysql_num_rows($suppDescription);
$allSupplierInfo=array();
for($i=0; $i<$rows; $i++){
$allSupplierInfo[]=mysql_fetch_row($suppDescription);
}
使用while loop
也可以轻松完成,然后您只需处理返回的内容,而无需费心获取行数。
$suppDescription=mysql_query($query);
$allSupplierInfo=array();
while ( $rows = mysql_fetch_row($suppDescription) ) {
$allSupplierInfo[]=mysql_fetch_row($suppDescription);
}