While Loop using PHP with a MySQL server

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.



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


I'll do...

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


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


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


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


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 while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) { echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ; } ?>


  • Called id for nil, which would mistakenly be 4 — if you really wanted the id of nil, use object_id
  • How to send values from html form to mail in table PHP
  • MASM x86 fastcall function declaration… how?
  • How to populate 2nd combobox(g:select) values on basis of 1st combobox(g:select)?
  • Australian Mobile number regular expression validation needs to allow space in between number
  • How often does Python switch threads?
  • How to parse multi-line headers of SIP message using regex?
  • How to retry only on certain error emitted by the source observable in RxJs
  • Python validation mobile number
  • Structured data not being picked up on telephone number
  • output json array in php
  • Entity framework 'include' including more than it should
  • Storing data from SQL in array
  • SSRS 2008 - Sorting within a group
  • Where these are stored?
  • Add dynamic data to line chart from mysql database with highcharts
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • Play WS (2.2.1): post/put large request
  • Dialing with Intent.ACTION_CALL stopps at # in phone number
  • Master page gives error
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • Do I've to free mysql result after storing it?
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • php design question - will a Helper help here?
  • Unanticipated behavior
  • Matrix multiplication with MKL
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • A cron job substitute?
  • json Serialization in asp
  • AngularJs get employee from factory
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • How can I remove ASP.NET Designer.cs files?
  • Can't mass-assign protected attributes when import data from csv file
  • How to Embed XSL into XML
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal