ellchr3 Posted December 15, 2011 Share Posted December 15, 2011 What's the best way to format results from a MySQL table? I've found very vague examples of PHP code utilizing html tables. Below is my current PHP. Thanks! <?php $con = mysql_connect("localhost","xxxxx","xxxxx") or die('Could not connect: ' . mysql_error()); mysql_select_db("addresses", $con); $result = mysql_query("SELECT * FROM addresses"); while($row = mysql_fetch_array($result)) { echo $row['first_name'] . " " . $row['last_name'] . " " . $row['extra_info'] . " " . $row['address'] . " " . $row['city'] . " " . $row['state'] . " " . $row['zip']; echo "<br />"; } mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/253240-formatting-mysql-results-in-php/ Share on other sites More sharing options...
marcus Posted December 15, 2011 Share Posted December 15, 2011 It really depends on how you want your data to be viewed. You can have a fancy layout or just a simple table. echo '<table border="0" cellspacing="3" cellpadding="3">'; echo "\n<tr><td>First Name</td>Last Name</td><td>etc...</td></tr>\n"; while($row = mysql_fetch_array($result)){ echo "<tr><td>".$row['first_name']."</td><td>".$row['last_name']."</td><td>".$row['etc']."</td></tr>\n"; } echo "</table>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/253240-formatting-mysql-results-in-php/#findComment-1298168 Share on other sites More sharing options...
Psycho Posted December 15, 2011 Share Posted December 15, 2011 This really isn't a PHP issue. You should first decide what you want your output to look like. THEN you should build the PHP code to generate that output. I would suggest building a basic HTML page with some mock data to put the content into the format you want. You can tweak whatever specifications you want until you are happy with the look. In my opinion: it is much easier to design outside of code and it is much easier to code when you have a set design. Trying to design within code can be done, but it routinely becomes more difficult than it needs to be. Quote Link to comment https://forums.phpfreaks.com/topic/253240-formatting-mysql-results-in-php/#findComment-1298177 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.