简体   繁体   English

Print_r 只显示第一行

[英]Print_r only show first row

So I have the following code :所以我有以下代码:

$sql = mysql_query("SELECT result FROM table1");
$result = mysql_fetch_array($sql);
print_r($result);

What I want to do here is only to check whether the data that selected above will be displayed or not, but the result from print_r only show the first row of the data.我这里要做的只是检查上面选择的数据是否会显示,但是print_r的结果只显示数据的第一行。

for example the result : Array ( [0] => 2 [result] => 2 )例如结果: Array ( [0] => 2 [result] => 2 )

When i check on the database, there's nothing wrong with the data.当我检查数据库时,数据没有任何问题。 After the first row, there are still other rows.在第一行之后,还有其他行。
Maybe it's kinda stupid question, but can someone help me?也许这有点愚蠢的问题,但有人可以帮助我吗? Thank you.谢谢你。

Try a while to fetch all rows and not only the first one:尝试获取所有行,而不仅仅是第一行:

$sql = mysql_query("SELECT result FROM table1");
while ($row = mysql_fetch_array($sql)) {
    print_r($row); 
}

For every call of mysql_fetch_array it fetch only one row and when there are no more row is then you get false back.对于mysql_fetch_array每次调用,它只获取一行,当没有更多行时,你会得到错误返回。

The while loop and call for every loop mysql_fetch_array so long until they get false back. while 循环并为每个循环调用mysql_fetch_array这么长时间,直到它们返回 false 为止。

You can also write to get 2 rows, but then you have "static" code and nor more then 2 rows:你也可以写得到 2 行,但是你有“静态”代码,也不超过 2 行:

$sql = mysql_query("SELECT result FROM table1");
$row1 = mysql_fetch_array($sql);
$row2 = mysql_fetch_array($sql);

and if you have only one row in the table you get this results:如果表中只有一行,则会得到以下结果:

$row1 = Array ( [0] => 2, [result] => 2 );
$row2 = false;

I know you didn't ask for it but Do NOT use mysql_* as it has been removed, you can use PDO or mysqli instead like this.我知道你没有要求它但不要使用mysql_*因为它已被删除,你可以像这样使用PDOmysqli

<?php
$con=mysqli_connect("localhost","your_user_name","your_password","your_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="SELECT result FROM table1";
$result=mysqli_query($con,$sql);

// Fetch all rows as an associative array
$rows = mysqli_fetch_all($result,MYSQLI_ASSOC);

print '<pre>';    
print_r($rows);
print '</pre>';    

// Free result set
mysqli_free_result($result);

mysqli_close($con);
?>

With the following, you will get everything in one shot:使用以下内容,您将一次性获得所有内容:

$result = mysqli_fetch_all($sql, MYSQLI_ASSOC);

Works from v5.3.0+.从 v5.3.0+ 开始工作。 Sources: PHP.net and W3Schools资料来源: PHP.netW3Schools

Note that you would need to have MySQLnd as your native driver!请注意,您需要将 MySQLnd 作为您的本机驱动程序!

Available only with mysqlnd .仅适用于mysqlnd

To print it in a more structured way, here is how:要以更结构化的方式打印它,方法如下:

echo "<pre>";
print_r($result);
echo "</pre>";
$sql = mysql_query("SELECT result FROM table1");
$count = mysql_num_rows($sql);
if($count>0){
 //anything here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM