duanlieshuang5330 2013-03-19 18:34
浏览 175
已采纳

如何将作为mysql查询结果返回的列名的所有行存储在字符串数组中

I want to store all rows of a column name returned as a mysql query result in a string array. I am new to php. I have retrieved the rows but how do I store all rows of a column in an array? I mean how to iterate the counter of rows and store the column in an array? Or is there any direct function/method call? Please see my code below and its possible ways of storing in an array:

$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'password';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
$dbname = 'users';
mysql_select_db($dbname);

$result = mysql_query("SELECT users.email FROM users");

//  AND THEN FINALLY STORING IN A STRING ARRAY:

$array = $result;

//  OR SOMETHING LIKE:

Store($array, $result);

//  OR SOMETHING LIKE:

for(int i = 0; i < $result.Rows.Count; i++)
{
    if (null != $row[0])
    {
        $array[i] = $result[i];
    }
}

//  OR SOMETHING LIKE:

while($null != result.read())
{
    $array.Add($result.Read());
}

Please help me in writing the code.

  • 写回答

2条回答 默认 最新

  • duanquyong8164 2013-03-19 18:38
    关注

    MySQL Fetch Array

    $result = mysql_query("SELECT users.email FROM users");
    
    while($row = mysql_fetch_array($result)){
        $array[] = $row;
    }
    

    This will return an array of associated arrays. The $row will contain the index of the column name along with integer index.

    For example:

    echo $row['email'];
    //or
    echo $row[0];
    

    In the end you can get the first row by:

    $array[0]['email']; //or $array[0][0];
    

    NOTICE: Do not use MySQL_* for it has been deprecated as of PHP 5.5. Use MySQLi_* or PDO instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥15 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)