php 如何将sql查询结果放入数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13793724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:05:12  来源:igfitidea点击:

How to put sql query result into an array?

phpmysql

提问by Newbie

I want to put a sql query result into an array. I tried the code below but it shows the 1st record for $count times. Obviously it's something wrong at the "$dept[$i]= $row['name'];". But i have no idea how to fix it. Somebody help please?

我想将一个 sql 查询结果放入一个数组中。我尝试了下面的代码,但它显示了 $count 次的第一条记录。显然,“$dept[$i]= $row['name'];”有问题。但我不知道如何解决它。有人帮忙吗?

$sql="SELECT name FROM system_dept ORDER BY id";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
$count=mysql_num_rows($result);
if (!mysql_query($sql,$con))
{
    die('Error: ' . mysql_error());
}
else
{
    $dept = array();
    $i=0;

    for($i=0;$i<$count;$i++)
    {
        $dept[$i]= $row['name'];
        echo $dept[$i];
    }
}

Ok, i tried to use mysqli but it doesnt work. the web server shows that: MySQL client version: 4.1.22 PHP extension: mysql Can mysqli works in mysql php extension?

好的,我尝试使用 mysqli 但它不起作用。Web 服务器显示: MySQL 客户端版本:4.1.22 PHP 扩展:mysql mysqli 可以在 mysql php 扩展中工作吗?

采纳答案by Darwayne

you are only fetching one row to fetch more rows you'll need to call fetch result again so just add this to your code and things should be fine:

您只获取一行以获取更多行,您需要再次调用 fetch result ,因此只需将其添加到您的代码中,事情应该没问题:

for($i=0;$i<$count;$i++)
    {
        $dept[$i]= $row['name'];
        echo $dept[$i];
        $row=mysql_fetch_array($result);
    }

I would recommend using a while loop instead like the following:

我建议使用 while 循环,如下所示:

$sql="SELECT name FROM system_dept ORDER BY id";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if (!mysql_query($sql,$con))
{
    die('Error: ' . mysql_error());
}
else
{
    $dept = array();
    while($row=mysql_fetch_array($result))
    {
      $dept[]=$row['name'];
      echo $row['name'];
     }
}

If at all possible look into mysqli and PDO, as they are both more efficient

如果可能的话,看看 mysqli 和 PDO,因为它们都更有效率

回答by markus

Why are you only fetching one row, when you actually want all rows?

为什么您只获取一行,而您实际上想要所有行?

With mysqli:

使用 mysqli:

$db = new mysqli("localhost", "foo_dbo", "pass", "foo_db");    
$result = $db->query("SELECT name FROM system_dept ORDER BY id");    
$names = $result->fetch_all(MYSQLI_ASSOC);

With PDO:

使用 PDO:

$db = new PDO('mysql:dbname=foo_db;dbhost=localhost', 'foo_dbo', 'pass');
$stmt = $db->query("SELECT name FROM system_dept ORDER BY id");
$names = $stmt->fetchAll(PDO::FETCH_ASSOC);

This leads to the same result as the other answers but without the loop! It's faster! If you fetch row by row, you're slower but depending on what you do, you'll need less memory. If you're storing each row in an array, this will not be the case. So fetching all is the way to go.

这导致与其他答案相同的结果,但没有循环!它更快!如果您逐行获取,速度会变慢,但根据您的操作,您将需要更少的内存。如果您将每一行存储在一个数组中,则情况并非如此。所以获取所有是要走的路。

回答by Dejan Marjanovic

$dept = array();
$result = mysql_query('SELECT name FROM system_dept ORDER BY id');
while ($row = mysql_fetch_assoc($result))
{
  $dept[] = $row['name'];
  echo $row['name'];
}

Please, don't use mysql_*functions to write new code. They are no longer maintained and the community has begun deprecation process. See the red box? Instead you should learn about prepared statementsand use either PDOor MySQLi. If you can't decide which, this articlewill help you. If you pick PDO, here is good tutorial. Also see Why shouldn't I use mysqlfunctions in PHP?

请不要使用mysql_*函数来编写新代码。它们不再被维护,社区已经开始弃用过程。看到红框了吗?相反,您应该了解准备好的语句并使用PDOMySQLi。如果你不能决定哪个,这篇文章会帮助你。如果您选择 PDO,这里有很好的教程。另请参阅为什么我不应该mysql在 PHP 中使用函数?