Page 1 of 1

Creating a table with MySQL return results

Posted: Thu Sep 07, 2006 8:21 am
by impulse()
http://stesbox.co.uk/php/db/retrieve.php

I have managed to get PHP to create a table from some data but I can only get it to create the first row and the rest it ouputting unformatted. Any ideas on what's going wrong here?

Code: Select all

<table border="2" bgcolor="grey">
  <caption><b> People</b> </caption>
      <tr>
        <th bgcolor="black"><font color="white"> Key </th>
        <th bgcolor="black"><font color="white"> Forname </th>
        <th bgcolor="black"><font color="white"> Surname </th>
        <th bgcolor="black"><font color="white"> Age </th>
        <th bgcolor="black"><font color="white"> Sex </th></a>
      </tr>

<?php
$i = 0;
while ($i < $num) {
  $key = mysql_result($result, $i, "ref");
  $first = mysql_result($result, $i, "fName");
  $last = mysql_result($result, $i, "sName");
  $age = mysql_result($result, $i, "age");
  $sex = mysql_result($result, $i, "sex");

?>
    <td bgcolor = "white"> <?php echo "$key"; ?> </td>
    <td bgcolor = "white"> <?php echo "$first"; ?> </td>
    <td bgcolor = "white"> <?php echo "$last"; ?> </td>
    <td bgcolor = "white"> <?php echo "$age"; ?> </td>
    <td bgcolor = "white"> <?php echo "$sex"; $i++ ?> </td>

  </table>
</html>


<?php
}
[/url]

Posted: Thu Sep 07, 2006 8:30 am
by feyd
Lose the calls to mysql_result(). Use mysql_fetch_array(), mysql_fetch_row() or mysql_fetch_assoc(). My preference is for the latter.

Posted: Thu Sep 07, 2006 8:41 am
by impulse()
Is there a flaw in mysql_result that disallows you to create a table with more than 1 row or are you offering friendly performance advice?