将mysql列转换为php数组并回显它(Turning a mysql column into a php array and echoing it)

编程入门 行业动态 更新时间:2024-10-26 10:31:19
将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'];

更多推荐

本文发布于:2023-07-29 20:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319598.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   转换为   php   mysql   echoing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!