-1

I've been working on a project that prints sql data into a table. Recently, I've come into a problem with the table. What this code should do is output a table of results from a MySQL query but all it's outputting is something like:

Item1                                              Item1

For some reason it leaves all the other fields blank. Here's my code:

$table = "<table class='TestTable'><tr class='tr'>";
while($row = mysql_fetch_assoc($result)){
    $table .= "<th class='th'>";
    $table .= $row['NameOfItem'];
    $table .= "</th>";
}
$table .= "</tr><tr class='tr'>";
while ($row = mysql_fetch_assoc($result)){
    $table .= "
        <td class='td'>Minimum Bid: <b>";
        $table .= $row['MinBid'];
        $table .= "</b></td>";
}
$table .= "</tr><tr class='tr'>";
while ($row = mysql_fetch_assoc($result)){
    $table .= "<td class='td'>Current Bid: <b>";
        $table .= $row['CurrentBid'];
        $table .= "</b></td>";
}
$table .= "</tr><tr class='tr'>";
while ($row = mysql_fetch_assoc($result)){
    $table .= "<td class='td'>Sold By: <b>";
        $table .= $row['SoldBy'];
        $table .= "</b></td>";
}
$table .= "</tr><tr class='tr'>";
while ($row = mysql_fetch_assoc($result)){
    $table .= "<td class='td'>Time Left: <b>";
        $table .= printf('%d days, %d hours, %d minutes left', $diff->d, $diff->h, $diff->i);
        $table .= "</b></td>";
}
$table .= "</tr></table>";
echo $table;

When I view source I get:

<table class='TestTable'>
    <tr class='tr'>
        <th class='th'>Item1</th>
        <th class='th'>Item1</th>
    </tr>
    <tr class='tr'></tr>
    <tr class='tr'></tr>
    <tr class='tr'></tr>
    <tr class='tr'></tr>
</table>
maxhb
  • 8,554
  • 9
  • 29
  • 53
User2001
  • 15
  • 7
  • RTM: [mysql_fetch_assoc](http://php.net/manual/en/function.mysql-fetch-assoc.php): _Returns an associative array of strings that corresponds to the fetched row, **or FALSE if there are no more rows.**_ All rows are read in the first `while` loop... So in the next `while`, there are no more rows to read... – FirstOne Jan 01 '16 at 18:19
  • erm, I think there are issues here. Once you have iterated through the recordset in the first loop you would need to rewind the recordset pointer to the first row again. Why so many loops? – Professor Abronsius Jan 01 '16 at 18:20
  • you can only do *once* `while ($row = mysql_fetch_assoc($result))`. After that you're at the end of $result, hence have no more rows to return. Also update to msqli or pdo! msql_ functions are deprecated – Jeff Jan 01 '16 at 18:21
  • The way it appears you are trying to generate a table with a row of headers ( `th` ) and then loop though the recordset multiple times ( but to what end? ) ~ After you generate the table headers use `mysql_data_seek($result,0)` to return to the first row in results and then you should be able to render the rest of the table in one loop – Professor Abronsius Jan 01 '16 at 18:25
  • Sidenote: I know you can put whatever name you want, but using `` is kinda redundant... – FirstOne Jan 01 '16 at 18:28
  • Ok thanks, I've fixed it now I'll delete this question now. Thanks for the help. – User2001 Jan 01 '16 at 18:29

2 Answers2

0

After while($row = mysql_fetch_assoc($result)) {...} you have reached the end of your result set. You have to rewind the result pointer to the beginning of the result set if you want to read it again. Do this by using

while($row = mysql_fetch_assoc($result)){ ... }
mysql_data_seek($result, 0); // Rewind result pointer
while($row = mysql_fetch_assoc($result)){ ... }
mysql_data_seek($result, 0); // Rewind result pointer
while($row = mysql_fetch_assoc($result)){ ... }
maxhb
  • 8,554
  • 9
  • 29
  • 53
  • 3
    A general note: Don't use mysql_* functions. They are outdated. Switch to mysqli_* functions. – maxhb Jan 01 '16 at 18:27
0

First get your results and safe them in an array:

while($r = mysql_fetch_assoc($result)){
    $row[] = $r;
}

Then reuse the array as often as needed to build the table like this:

$table .= "</tr><tr class='tr'>";
foreach ($row as $r){
    $table .= "
        <td class='td'>Minimum Bid: <b>";
        $table .= $r['MinBid'];
        $table .= "</b></td>";
}

... and yes, mysql-functions are deprecated. When you move to PHP7 they'll be gone. Use PDO or mysqli instead.