2

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!

3
  • 1
    $row.length is not valid PHP. Commented Feb 22, 2012 at 13:42
  • What is it displaying right now? Commented Feb 22, 2012 at 13:42
  • Nothing, but removing the while loop would display the first record. Commented Feb 22, 2012 at 13:43

7 Answers 7

16

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"] ;
}
Sign up to request clarification or add additional context in comments.

1 Comment

NOTE: 'mysql_fetch_array' was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. PHP 5.x is also deprecated; so, this method should no longer be used.
2

I'll do...

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

Comments

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

Comments

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

Comments

0

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"] ;
  }
?>

Comments

0

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.

Comments

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

1 Comment

mysql and mysqli both are different. You are correct if he was using mysqli instead of mysql :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.