Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP/MySQL query into html table issue
    primarykey
    data
    text
    <p>I'm having a strange issue with putting a query into a html table to be displayed in a php/mysql set up. The query will always produce one more result than is shown. For example, if 1 set of results was returned, nothing would show in the table. If there are 5, only 4 get shown. I've tried the same queries with a pre-defined function to produce a table based on a query and it produced the right table with the correct number of results. Here is an example (apologies for the indenting, or lack thereof):</p> <pre><code>$user_id = implode($_SESSION['user_id']); $query = "SELECT name, address FROM user WHERE user_id = '$user_id' ORDER BY date_added DESC"; $result = mysql_query($query) or die ("query failed: " . mysql_error()); echo "&lt;table border='1' style=\"border-collapse: collapse;\"&gt; &lt;tr style=\"background-color: #000066; color: #FFFFFF;\"&gt; &lt;th&gt;Name&lt;/th&gt; &lt;th&gt;Address&lt;/th&gt; &lt;/tr&gt;"; $rowCt = 0; // Row counter while($row = mysql_fetch_array($result)) { if($rowCt++ % 2 == 0) $Style = "background-color: #00CCCC;"; else $Style = "background-color: #0099CC;"; echo "&lt;tr style=\"$Style\"&gt;"; echo "&lt;td&gt;" . $row['name'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['address'] . "&lt;/td&gt;"; echo "&lt;/tr&gt;"; } echo "&lt;/table&gt;"; </code></pre> <p>Any idea why this is happening? Any help would be greatly appreciated, it's been driving me crazy and need to figure it out before I can move on! Thanks.</p> <p><em>EDIT</em></p> <p>I've altered it slightly to loop through the amount of rows as that number is correct, and the table is now the correct size, however the last result is always empty. Any ideas?</p> <pre><code>$rowCt = 0; // Row counter $i = 0; echo mysql_num_rows($result). ' number of rows'; $num = mysql_num_rows($result); //while($row = mysql_fetch_array($result)) while ($i &lt; $num) { $row = mysql_fetch_array($result); if($rowCt++ % 2 == 0) $Style = "background-color: #00CCCC;"; else $Style = "background-color: #0099CC;"; echo "&lt;tr style=\"$Style\"&gt;"; echo "&lt;td&gt;" . $row['name'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['address'] . "&lt;/td&gt;"; echo "&lt;/tr&gt;"; $i++; } echo "&lt;/table&gt;"; </code></pre> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload