mcc_22ri Posted May 17, 2012 Share Posted May 17, 2012 Hi Everyone, I'm trying to figure out a way to put my MySql data into a table. I'm close to getting this completed by I'm having some trouble. The data I'm inserting isn't being put into the correct tables. I'm not quite sure what I'm doing wrong. Any help? http://whatsmyowncarworth.com/practice/table.php <?php include('init.php'); $result = mysql_query("SELECT * FROM info") or die(mysql_error()); echo "<table border='1'>"; echo "<tr> <th>Name</th> <th>Last Name</th> <th>Address</th> <th>City</th> </tr>"; // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table echo "<tr><td>"; echo $row['first_name']; echo "</td><td>"; echo $row['last_name']; echo "</td></tr>"; echo "<tr><td>"; echo $row['address']; echo "</td><td>"; echo $row['city']; echo "</td></tr>"; } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/262690-putting-information-into-a-table/ Share on other sites More sharing options...
mrMarcus Posted May 17, 2012 Share Posted May 17, 2012 Your HTML is the issue. You have your <tr>'s firing too soon. Quote Link to comment https://forums.phpfreaks.com/topic/262690-putting-information-into-a-table/#findComment-1346408 Share on other sites More sharing options...
mrMarcus Posted May 17, 2012 Share Posted May 17, 2012 In addition, I find it's easier to close your PHP portion and write the HTML so it's more legible than echo'ing each instance of HTML markup: <?php include('init.php'); $sql = "SELECT * FROM `info`"; if ($result = mysql_query($sql)) { echo "<table border='1'>"; echo "<tr> <th>Name</th> <th>Last Name</th> <th>Address</th> <th>City</th> </tr>"; // keeps getting the next row until there are no more to get while ($row = mysql_fetch_array($result)) { // Print out the contents of each row into a table ?> <tr> <td><?php echo $row['first_name']; ?></td> <td><?php echo $row['last_name']; ?></td> <td><?php echo $row['address']; ?></td> <td><?php echo $row['city']; ?></td> </tr> <?php } echo "</table>"; } else { trigger_error(mysql_error()); // for development only; remove when in production } Quote Link to comment https://forums.phpfreaks.com/topic/262690-putting-information-into-a-table/#findComment-1346410 Share on other sites More sharing options...
mcc_22ri Posted May 17, 2012 Author Share Posted May 17, 2012 Thanks mrMarcus. The code is very clear, I appreciate your help! Quote Link to comment https://forums.phpfreaks.com/topic/262690-putting-information-into-a-table/#findComment-1346411 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.