Jump to content

[SOLVED] echo all data in mysql table


fry2010

Recommended Posts

Ok this is a real simple one. I dont know why I cant get it working, I can normally do loops etc to echo data in table but this isnt working...

 

  $conn = db_connect();
  $result = $conn->query("SELECT * FROM notice_board");
  $result2 = $conn->query("SELECT COUNT(*) FROM notice_board");
  $row = $result->fetchObject();
  $column = $result2->fetchColumn();

  $i = 0;
  while($i < $column)
  {
    echo '<tr>';
    echo '<td>'.$row->id.'</td><td>'.$row->type.'</td><td>'.$row->entry.'</td><td>'.$row->sl.'</td><td>'.$row->tp.'</td><td>'.$row->notes.'</td><td>'.$row->changes.'</td>';
    echo '</tr>';
    $i++;
  }

  $conn = NULL;

 

This will produce a table with 3 rows in it with all the columns, the only problem is that it is repeating the first row in the database table.

 

So the id is just listed as number 1. Not 1,2,3.

 

Here is the output.

1	buy	1.9865	1.9865	1.9865	1.9865	
1	buy	1.9865	1.9865	1.9865	1.9865	
1	buy	1.9865	1.9865	1.9865	1.9865	

 

There is only three rows in the database so it has echoed the correct number of rows, just not each row.

Link to comment
https://forums.phpfreaks.com/topic/148461-solved-echo-all-data-in-mysql-table/
Share on other sites

  $conn = db_connect();
  $result = $conn->query("SELECT * FROM notice_board");
  $result2 = $conn->query("SELECT COUNT(*) FROM notice_board");

  while($row = $result->fetchObject())
  {
    $column = $result2->fetchColumn();
    echo '<tr>';
    echo '<td>'.$row->id.'</td><td>'.$row->type.'</td><td>'.$row->entry.'</td><td>'.$row->sl.'</td><td>'.$row->tp.'</td><td>'.$row->notes.'</td><td>'.$row->changes.'</td>';
    echo '</tr>';
  }

  $conn = NULL;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.