5

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):

$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; // Row counter
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's been driving me crazy and need to figure it out before I can move on! Thanks.

EDIT

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?

$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>";

Thanks

4
  • 1
    echo mysql_num_rows($result); and make sure it returns the number of rows you expect
    – Tom
    Commented Apr 1, 2012 at 1:40
  • 1
    what are you trying to do with 'implode($_SESSION['user_id'])' ?
    – mshsayem
    Commented Apr 1, 2012 at 1:50
  • I see nothing wrong with your code. Only in you while, you have an if and else, try commenting them out and look what happens, it could do something unexpected. Also i suggest to execute the $query directly in phpmyadmin (if installed) and see the returned dataset to confirm the returned data.
    – ArendE
    Commented Apr 1, 2012 at 2:02
  • I've tried to echo the number of rows, in this example it will echo 2 yet only display one result set, very strange. The implode statement is to retrieve the user id to complete the query. I've also tried removing the if/else to no avail, as well as executing the query in phpmyadmin, it returns the same number (one more than is displayed). Any ideas? Thanks for the replies Commented Apr 1, 2012 at 14:43

8 Answers 8

1

Try this.........

<style>
#s {
background-color: #000066; 
color: #FFFFFF;
}
#s1 {
background-color: #0CC;
}
#s2 {
background-color: #09C;
}
</style>    
<?php
$user_id = implode($_SESSION['user_id']);
$result = mysql_query("SELECT name, address FROM user WHERE user_id = '$user_id' ORDER BY date_added DESC", $your_connection) or die ("query failed: " . mysql_error());        
?>
<table border="1" style="border-collapse:collapse;">
<tr id="s">
<th>Name</th>
<th>Address</th>
</tr>
<?php
$rowCt = 0; // Row counter
while($row = mysql_fetch_assoc($result))
  {
  if($rowCt++ % 2 == 0):
    $Style = "s1";
  else: 
    $Style = "s2";
  endif;
  ?>
  <tr id="<?php echo $Style; ?>">
  <td><?php $row['name']; ?></td>
  <td><?php $row['address']; ?></td>
  </tr>
  <?php } ?>
  </table>
0

Have you declared a DOCTYPE on the page? This is the only thing I can think of. Your code looks correct.

Note: If a !DOCTYPE is not specified, the border-collapse property can produce unexpected results.

http://www.w3schools.com/cssref/pr_tab_border-collapse.asp

Also, you have warnings/errors turned on right? There may be some error with that last row which is why it's not displaying.

1
  • Hi, thanks for the reply. The doctype is declared and there are no errors related to that area showing. Even when I get it to count the amount of things returned, it still displays one less than is actually there! Any other ideas? Commented Apr 1, 2012 at 11:01
0

try using mysql_fetch_assoc instead of mysql_fetch_array

0

check your $i and $num in while loop. i think your while should be:

while($i <= $num){
// Your code
}
0

Try this:

    <?php
$user_id = $_SESSION['user_id'];
$query = mysql_query("SELECT * FROM `user` WHERE `user_id`='$user_id' ORDER BY `date_added` DESC")or die(mysql_error());
?>
<table border="1" style="border-collapse: collapse;">
<tr style="background-color: #000066; color: #FFFFFF;">
<th>Name</th>
<th>Address</th>
</tr>
<?
$colour2 = "transparent";  
$colour1 = "#0099CC";  
$row_count = 0; 
while($row = mysql_fetch_array($query)){
$rowcolor = ($row_count % 2) ? $colour1 : $colour2;
?>
<tr style="background: <? echo $rowcolor; ?>;">
<td><? echo $row['name']; ?></td>
<td><? echo $row['address']; ?></td>
</tr>
<?
$row_count++;
}
?>
</table>
0

Have you tried the output without table? ie

echo $row['name']." - ".$row['address']."<br />";

If yes with the same result, have you applied that query in phpMyAdmin to check the result? might be your last record is empty in the DB so the code displays nothing.

0

try using a for loop instead of while.

for($i=0;$i<=mysql_num_rows;$i++)
{
    $row = mysql_fetch_array($query);
    ...
    ...
}
0

Perhaps is it counting the

    <th>Name</th>
<th>Address</th>

As rows try, taking them out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.