Discussion

Welcome to Absolute Web Dev Forum. Feel free to Register and ask me a question about web development or, if you can answer or contribute to the aswering of someones question, feel free to respond. All listings are reviewed by me so please make your comments usefull. If you will, please leave links to related websites or other references that someone can go to for further information on a given subject. Thank you for your contribution to Absolute Web Devs Web Forum.Create Discussion

How do I display all of the files from a database using php?

JackStraw

How do I display all of the files from a database using php?   -   02/18/2009


I want do display everything in my database in tables. The current code I am using only displays 1 row of my data base. How do I get php to display all of my data, formatted in a table or some other kind of format?
Here is the code I am using now:
$sql = "SELECT * from books";
$result = mysql_query($sql,$dbc);
$row = mysql_fetch_array ($result, MYSQL_NUM);

echo '
<table width="200">
<tr><td>My Books</td></tr>
<tr><td>Id '. $row[0] .'</td></tr>
<tr><td>Title '. $row[1] .'</td></tr>
<tr><td>Author '. $row[2] .'</td></tr>
<tr><td>Date '. $row[3] .'</td></tr>
<tr><td>ISBN '. $row[4] .'</td></tr>
</table>';

Is there something missing or am I just doing it wrong.

Thanks!

WebMaster

Response   -   02/18/2009


You just need to stick it in a loop. I'll put your code into a while loop and it should work just as you want it.

$sql = "SELECT * from books";
$result = mysql_query($sql,$dbc);
echo '<table width="200">';
while ($row = mysql_fetch_array ($result, MYSQL_NUM)){
echo '
<tr><td>My Books</td></tr>
<tr><td>Id '. $row[0] .'</td></tr>
<tr><td>Title '. $row[1] .'</td></tr>
<tr><td>Author '. $row[2] .'</td></tr>
<tr><td>Date '. $row[3] .'</td></tr>
<tr><td>ISBN '. $row[4] .'</td></tr>
';
}
echo '</table>';

This echo's the table tags once and will repeat the table rows as long as there are records inside your database. I would change the format of the table. The way you have it displayed here is rather funky. Other than that that should do it. I hope this helps.

Add to this Discussion

Discussion Archive


TOP

  100% compatible with:
Firefox Explorer Opera Safari Flock Netscape Chrome

 100% valid CSS 3.0 and XHTML 1.0.
Valid CSS Valid XHTML