0

Hi I'm attempting to display data retrieved from a mysql table horizontally in an html table using php. The code below works well except for the fact that it leaves out the first record (starts at the second record) in my database. I'm sure it has something to do with the counter but I can't seem to figure out how to get it to stop doing this. If anyone can point out my error I'd really appreciate it!

$items = 5;
$query = "SELECT * FROM members ";

$result = mysql_query($query) 
  or die(mysql_error());

$row = mysql_fetch_array($result);

if (mysql_num_rows($result) > 0) {

echo '<table border="1">';

$i = 0;

while($row = mysql_fetch_array($result)){

     $first_name = $row['first_name'];

           if ($i==0) {
                       echo "<tr>\n";
                      }

           echo "\t<td align=\center\">$first_name</td>\n";

           $i++;

           if ($i == $items) {

                              echo "</tr>\n";
                              $i = 0;
                              }
 }//end while loop

 if ($i > 0) {

                 for (;$i < $items; $i++) {
                      echo "<td>&nbsp;</td>\n";
                  }

              echo '</tr>';

              }//end ($i>0) if 

 echo '</table>';

 }else {

   echo 'no records found';
 }
1
  • You shouldn't use mysql_ function, and your indentation is cumbersome Commented Jul 14, 2013 at 18:52

4 Answers 4

1

try and remove the 1st

$row = mysql_fetch_array($result);

you are calling it twice, that's why it skips 1 row in your while loop

Sign up to request clarification or add additional context in comments.

1 Comment

That's it thanks! I would have marked it as the correct answer but apparently I can't after only nine minutes.
1

try this simpler.

       $items = 5;
  $query = "SELECT * FROM members ";

 $result = mysql_query($query) or die(mysql_error());
 if (mysql_num_rows($result) > 0) {

 echo '<table border="1">';
 while($row = mysql_fetch_array($result)){

 $first_name = $row['first_name'];
        echo "<tr>";
       for ($i=0 ; $i <= $items ;$i++) {

                    echo "<td align='center'>".$first_name."</td>";
                    }
}//end while loop
     echo "</tr>";
    echo '</table>';
}else{ echo 'no records found'; }

1 Comment

@matttuman because i have removed \n and \t .try add them again and see
0

I have run into this issue before. Try the do while loop instead. Example

do {

// code

 } while($row = mysql_fetch_array($result)); //end while loop

Comments

0

$row = mysql_fetch_array($result); 1) remove this line of code from ur scripts 2) only use while loop code instead.

1 Comment

Please explain that further such that others can learn from your answer

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.