PHP / MySQL query to html table

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

+5
8

.........

<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>
+1

DOCTYPE ? , . .

. ! DOCTYPE , border-collapse .

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

, / ? , .

0

mysql_fetch_assoc mysql_fetch_array

0

$i $num in while. , :

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

:

    <?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

?

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

If yes with the same result, did you apply this query in phpMyAdmin to check the result? maybe your last entry is empty in the database, so the code does not display anything.

0
source

try using a for loop instead of a while.

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

Maybe it's a tally

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

As the lines try, taking them out.

0
source

All Articles