While Loop using PHP with a MySQL server

时间秒杀一切 提交于 2020-01-04 21:31:31

问题


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.

<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.

Thanks!


回答1:


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.

while ($row = mysql_fetch_array($result))  
{
    echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ;
}



回答2:


I'll do...

while ($row = mysql_fetch_assoc($result)) {
   // print $row;
}



回答3:


while ($row = mysql_fetch_array($result))  
    {
        echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ;
    }



回答4:


<?php
$qry = mysql_query($result); 
while ($row = mysql_fetch_array($qry))  
    {
        echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ;
    }
?>



回答5:


PHP has great documentation with examples. Check out the example for 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"] ;
  }
?>



回答6:


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.




回答7:


<?php
  while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
     echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ;
  }
?>


来源:https://stackoverflow.com/questions/9395941/while-loop-using-php-with-a-mysql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!