I had a weird problem with putting the request into the html table for display in php / mysql setting. The query will always produce one more result than shown. For example, if 1 result set was returned, nothing will be shown in the table. If there are 5 of them, only 4 is displayed. I tried the same queries with a predefined function to create a table based on the query and created the right table with the right amount of results. Here is an example (apologies for the indentation or absence):
$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 "<table border='1' style=\"border-collapse: collapse;\">
<tr style=\"background-color: #000066; color: #FFFFFF;\">
<th>Name</th>
<th>Address</th>
</tr>";
$rowCt = 0;
while($row = mysql_fetch_array($result))
{
if($rowCt++ % 2 == 0) $Style = "background-color: #00CCCC;";
else $Style = "background-color: #0099CC;";
echo "<tr style=\"$Style\">";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "</tr>";
}
echo "</table>";
Any idea why this is happening? Any help would be greatly appreciated, it drove me crazy and I had to understand this before I could move on! Thank.
EDIT
, , , , . ?
$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 < $num)
{
$row = mysql_fetch_array($result);
if($rowCt++ % 2 == 0) $Style = "background-color: #00CCCC;";
else $Style = "background-color: #0099CC;";
echo "<tr style=\"$Style\">";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "</tr>";
$i++;
}
echo "</table>";