将mysql列转换为php数组并回显它(Turning a mysql column into a php array and echoing it)
当我尝试从MySQL中的表中选择一列并将其转换为PHP数组时,我遇到了一些问题。 当我回显我的结果(PHP数组)时,它不会返回任何东西。
这是我的代码:
$result_while = mysql_query("SELECT id` FROM afunda_eleva"); $array = array(); while ($row = mysql_fetch_array($result_while)) { $array[] = $result_while['id']; } for($i=0; $i < 4; $i++) { echo "$array[$i]"; //Possibly error happening here!! }I'm having some problem when I try to select a column from my table in MySQL and turn it into a PHP array. When I echo my result (the PHP array), it doesn't return me anything.
Here is my code:
$result_while = mysql_query("SELECT id` FROM afunda_eleva"); $array = array(); while ($row = mysql_fetch_array($result_while)) { $array[] = $result_while['id']; } for($i=0; $i < 4; $i++) { echo "$array[$i]"; //Possibly error happening here!! }最满意答案
首先,如果您希望在数组中使用列名,则需要将mysql_fetch_array替换为mysql_fetch_assoc 。
然后你需要访问while循环中的行,而不是结果:
$array[] = $row['id'];First if you wish to work with column names in your array you need to replace mysql_fetch_array with mysql_fetch_assoc.
Then you need to access the row, not the result, inside your while loop:
$array[] = $row['id'];更多推荐
发布评论