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条)

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器