在 MySQL 服务器上使用 PHP 进行循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9395941/
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
While Loop using PHP with a MySQL server
提问by Chrisosaurus
I have a database (SQL) with the table "Staff" with two records in it. I am required to display the contents of these records on a web page using PHP.
我有一个数据库(SQL),其中包含“Staff”表,其中有两条记录。我需要使用 PHP 在网页上显示这些记录的内容。
<html>
<head>
<title>CES Staff details</title>
</head>
<body>
**code emitted**
<?php
$row = mysql_fetch_array($result) ;
$looping = 1;
$i = $row.length;
while ($looping <= $i)
{
echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ;
$looping++;
}
?>
</body>
</html>
How would I change the while loop correctly so that it will display both records on the page.
我将如何正确更改 while 循环,以便在页面上显示两条记录。
Thanks!
谢谢!
回答by Michael Berkowski
mysql_fetch_array()
only retrieves a single row from the database. You need to call it inside your while
loop to retrieve all rows. The $looping
increment is unnecessary here, since mysql_fetch_array()
returns false when no more rows are available.
mysql_fetch_array()
只从数据库中检索一行。您需要在while
循环内调用它以检索所有行。该$looping
增量是不必要在这里,因为mysql_fetch_array()
返回false时,没有更多行可供选择。
while ($row = mysql_fetch_array($result))
{
echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ;
}
回答by Napolux
I'll do...
我会去做的...
while ($row = mysql_fetch_assoc($result)) {
// print $row;
}
回答by Jasper De Bruijn
while ($row = mysql_fetch_array($result))
{
echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ;
}
回答by jasonlfunk
PHP has great documentation with examples. Check out the example for mysql_fetch_array().
PHP 有很好的文档和示例。查看mysql_fetch_array()的示例。
Your code should look like this:
您的代码应如下所示:
<?php
while($row = mysql_fetch_array($result)) {
echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ;
}
?>
回答by Djumaka
Use this
用这个
while($row = mysql_fetch_array($result))
{
echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ;
}
In php there is no such thing like $row.length; the "." is an operator for string concatenation. Read more on mysql_fetch_array at http://php.net/manual/en/function.mysql-fetch-array.php.
在 php 中没有像 $row.length; 这样的东西。这 ”。” 是字符串连接的运算符。在http://php.net/manual/en/function.mysql-fetch-array.php阅读有关 mysql_fetch_array 的更多信息。
回答by Aman
<?php
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ;
}
?>
回答by JConstantine
<?php
$qry = mysql_query($result);
while ($row = mysql_fetch_array($qry))
{
echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ;
}
?>