PHP Display Mysql Rows with Column Limit
By Brian on Nov 06, 2009 with Comments 36
This little script is very useful for having a set width of a table and displaying several rows organized in that table.
It is easy to just display a table full of rows for all the data in a mysql table. Something like this:
<?php
$sql = mysql_query("SELECT * FROM table1");
if(mysql_num_rows($sql) > 0){
echo "<table width=\"500\" border=\"1\" cellpadding=\"3\" cellspacing=\"0\">";
echo "<tr>";
while($row = mysql_fetch_array($sql)){
echo "<td width=\"100\" align=\"center\">".$row["col1"]."</td>";
}
echo "</tr></table>";
}
?>
This will grab all content of the mysql table and put the data from column 1 as a table column in this table.
Let’s say we have some images we want to display. The table is 500 pixels wide. So if your images are no larger than 100 pixels, you would want to display 5 columns per row in your table. This will give it good organization and flow in your webpage.
<?php
$sql = mysql_query("SELECT * FROM table1");
if(mysql_num_rows($sql) > 0){
echo "<table width=\"500\" border=\"1\" cellpadding=\"3\" cellspacing=\"0\">";
echo "<tr>";
for($i=0;$i<=mysql_num_rows($sql);$i=$i+1){
while($row = mysql_fetch_array($sql)){
$i++;
if($i%5==0){
// use the operator to find the remainder of $i / 5 (%)
// if the remainder is 0; it is a multiple of 5, so make a new row.
echo "<td width=\"100\" align=\"center\"><img src=\"".$row["col1"]."\" border=\"0\" style=\"max-width:100px\" /></td></tr><tr>";
} else {
echo "<td width=\"100\" align=\"center\"><img src=\"".$row["col1"]."\" border=\"0\" style=\"max-width:100px\" /></td>";
}
}
}
echo "</tr></table>";
}
?>
The two codes will appear like so:
Before Column Limit:
| Image 1 | Image 2 | Image 3 | Image 4 | Image 5 | Image 6 | Image 7 | Image 8 | Image 9 | Image 10 |
After Column Limit:
| Image 1 | Image 2 | Image 3 | Image 4 | Image 5 |
| Image 6 | Image 7 | Image 8 | Image 9 | Image 10 |
Popularity: 21% [?]
Filed Under: Tutorials

Fascinating article with A number of useful advises. Thank you very much, I?l Begin Examining your blog.
Nice post, cool site, keep it up.
information to my Company
Please keep it up. I look for your Articles on my iphone rss feed every day.
Data to my Company
Please Maintain it up. I look for your Articles on my iph1 rss feed every Day time.
Nice blog. It’s everything here exactly what I need to know.
The blog was absolutely fantastic! Lots of great information and inspiration, both of which we all need!
I do agree with all the ideas you have presented in your post. They are very convincing and will definitely work. Thanks for the post.
Great post, I concur completely and appreciate the time you took to write it. Cheers!
I like to require breaks during the day and browse by means of some blogs to see what others are saying. This weblog appeared in my searches and I could not help but clicking on it. I’m happy I did because it was a very enjoyable read.
thanks !! quite helpful post!
Excellent summary, bookmarked the site with hopes to read more!
Pretty useful post. I’m doing research on this vocation right now and this helped. Thanks for your unique take on this topic.
A thoughtful insight and ideas I will use on my blog. You’ve obviously spent a lot of time on this. Congratulations!
Dude, I absolutely liked reading this post. You have convinced me to subscribe to your blog, but where can I find the RSS feed?
Good blog post! I love your weblog, is it okay to try it for Rss or atom?
This is the simplest code to select and display records from MySQL database table and display in PHP.
$cn=mysql_connect($db_host,$db_user,$db_password) or die(mysql_error());.
mysql_select_db($db_name,$cn) or die(mysql_error());.
$sql = “SELECT field_name FROM table_name”;.
$rs = mysql_query($sql) or die(mysql_error());.
while($row = mysql_fetch_array($rs)){.
echo $field_name = $row["field_name"];.
echo “</br>”;
}
mysql_free_result($rs);
Source:
<a href="http://phphelp.co/2012/04/26/how-to-select-and-display-mysql-records-data-in-php/">http://phphelp.co/2012/04/26/how-to-select-and-display-mysql-records-data-in-php/</a>
OR
<a href="http://addr.pk/acfd">http://addr.pk/acfd</a>