Neilsaab Posted November 16, 2007 Share Posted November 16, 2007 Hi guys, I'm new to using php and MySQL and I'm stuck on one small part of creating the table I want. I have created a database which will store details of cars for sale. In the table is a link to a photo of the car. It took me a while, as I said I'm new to this, but I managed to get some code to enable me to show the pic in a table. Because of the way I've done this I'm struggling to get the table to show more than the first row. Could someone show me how to use a loop to print out all the rows from the dbase, or correct the coding I have done. Thanks in advance. Here is my code: <? //make the database connection $conn = mysql_connect("localhost", "neil", "Norwich1969") or die('Could not connect: ' . mysql_error()); $select = mysql_select_db("nigelcars", $conn) or die('Could not select database'); //create query $sql = "SELECT * FROM cars"; $result = mysql_query($sql) or die('Query failed: ' . mysql_error()); $mainRow = mysql_fetch_assoc($result); $num_rows = mysql_num_rows($result); echo "$num_rows Rows\n"; $carId = $mainRow["carid"]; $pic = $mainRow["pic"]; $carDesc = $mainRow["cardesc"]; $price = $mainRow["price"]; //print table print "<table width = 90% cellpadding=10 cellspacing=0 border=2>\n"; print "<tr><td align=center><b>ID</b></td><td align=center><b>Photo</b></td> <td align=center><b>Description</b></td><td align=center><b>Price £</b></td> </tr>"; print "<tr>\n"; print "<td align = center>$carId</td>"; print "<td align = center><img src=$pic></td>"; print "<td align = center>$carDesc</td>"; print "<td align = center>$price</td>"; print "</table>\n"; ?> Quote Link to comment Share on other sites More sharing options...
atlanta Posted November 16, 2007 Share Posted November 16, 2007 <?php //make the database connection $conn = mysql_connect("localhost", "neil", "Norwich1969") or die('Could not connect: ' . mysql_error()); $select = mysql_select_db("nigelcars", $conn) or die('Could not select database'); //create query $sql = "SELECT * FROM cars"; $result = mysql_query($sql) or die('Query failed: ' . mysql_error()); $num_rows = mysql_num_rows($result); echo "$num_rows Rows\n"; while($mainRow = mysql_fetch_assoc($result)) { $carId = $mainRow["carid"]; $pic = $mainRow["pic"]; $carDesc = $mainRow["cardesc"]; $price = $mainRow["price"]; //print table print "<table width = 90% cellpadding=10 cellspacing=0 border=2>\n"; print "<tr><td align=center><b>ID</b></td><td align=center><b>Photo</b></td> <td align=center><b>Description</b></td><td align=center><b>Price £</b></td> </tr>"; print "<tr>\n"; print "<td align = center>$carId</td>"; print "<td align = center><img src=$pic></td>"; print "<td align = center>$carDesc</td>"; print "<td align = center>$price</td>"; print "</table>\n"; } ?> Quote Link to comment Share on other sites More sharing options...
revraz Posted November 16, 2007 Share Posted November 16, 2007 You can also eliminate this $carId = $mainRow["carid"]; $pic = $mainRow["pic"]; $carDesc = $mainRow["cardesc"]; $price = $mainRow["price"]; If you just add those 4 Arrays in your print "<td align = center>$carId</td>"; print "<td align = center><img src=$pic></td>"; print "<td align = center>$carDesc</td>"; print "<td align = center>$price</td>"; code. Quote Link to comment Share on other sites More sharing options...
Neilsaab Posted November 16, 2007 Author Share Posted November 16, 2007 That is fantastic guys. Thanks a lot. What a fast response as well I knew I needed to use a while loop, just wasn't sure how to set it up with the variables. Again thanks, I'll definately be sticking around these forums in case I get stuck again. Neil Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.