php Mysql 数据库检索多行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6500993/
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-26 00:34:33  来源:igfitidea点击:

Mysql database retrieve multiple rows

phpmysql

提问by user780483

I use a mysql database. When I run my query I want to be able to put each row that is returned into a new variable. I dont know how to do this.

我使用mysql数据库。当我运行查询时,我希望能够将返回的每一行放入一个新变量中。我不知道该怎么做。

my current code:

我目前的代码:

<?php
$result=mysql_query("SELECT * FROM table WHERE var='$var'");

$check_num_rows=mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result))
{

$solution=$row['solution'];
}
?>

The thing is that check num rows can return a row of an integer 0-infinity. If there are more solutions in the database how can I assign them all a variable. The above code works fine for 1 solution, but what if there are more? Thanks.

问题是 check num rows 可以返回整数 0-infinity 的行。如果数据库中有更多解决方案,我如何为它们分配一个变量。上面的代码适用于 1 个解决方案,但如果有更多解决方案呢?谢谢。

回答by zlkn

You can't give each variable a different name, but you can put them all in an array ... if you don't know how this works I suggest looking at a basic tutorial such as http://www.w3schools.com/php/php_arrays.aspas well as my code.

你不能给每个变量一个不同的名字,但你可以把它们都放在一个数组中......如果你不知道这是如何工作的,我建议看一个基本的教程,比如http://www.w3schools.com /php/php_arrays.asp以及我的代码。

A very simple way (obviously I haven't included mysql_num_rows etc):

一个非常简单的方法(显然我没有包括 mysql_num_rows 等):

$solutions = array()

while($row = mysql_fetch_assoc($result)) {
  $solutions[] = $row['solution'];
}

If you have three in your result solutions will be:

如果您的结果中有三个解决方案将是:

$solutions[0] -> first result $solutions[1] -> second $solutions[2] -> third

$solutions[0] -> 第一个结果 $solutions[1] -> 第二个 $solutions[2] -> 第三个

回答by Nathan Romano

 <?php
      $result=mysql_query("SELECT * FROM table WHERE var='$var'");
      $solution = array();

      $check_num_rows=mysql_num_rows($result);

      while ($row = mysql_fetch_assoc($result))
      {
           $solution[]=$row['solution'];
      }
 ?>