tags:

views:

294

answers:

5

How do you get php to show 5 rows from mysql

Then create a new line and show another 5 ect....

Thank you

+3  A: 

Use the LIMIT clause if you want to limit the amount of results returned from the query.

If you want to print an <hr/> after every fifth record you can check it via the modulus operator:

$counter = 1;

while ($row = mysql_fetch_assoc($rst)) {

 // print $row stuff

 if ($counter % 5 == 0)
    print "<hr />";

 $counter++;

}

Basically, we have a variable used to count how many records we've printed. Once that counter can be divided by five, and leave no remainder, we print our horizontal-rule.

Jonathan Sampson
No need to reset the counter. Let it increment and at least you have potentially useful information at the end of the loop, which is the total number of rows. Resetting the counter doesn't do anything useful other than taking up more cputime. And no matter how much the int grows, an int will always take up the same amount of space in memory weather it's 1, 100, or 1,000,000.
Wadih M.
You're right - answered too quickly :)
Jonathan Sampson
A: 

Err.. you mean something like:

SELECT * FROM `tablename` WHERE ... LIMIT 5
MiffTheFox
+1  A: 

Something like this is maybe helpfull:

  $result = mysql_query($query);  
  if($result){
    while($row = mysql_fetch_assoc($result)){ 
      if(++$i%5==0 && $i>0){
        /* do the extra thing... new line some styles */
      }
    }
  }
merkuro
A: 
$total = 20;//get total number here;
$limit = 5;
for($i = 0;$i< $total/$limit;$i++)
{
$sql = $result = $rows = "";
$start = $limit * $i;
$sql = "select * from m_table order by id desc limit $start,$limit";
$result = mysql_query($query);
while($rows = mysql_fetch_assoc($result))
{
    //print the result here;
}
}

so,you can fetch 5 rows every time from mysql,but not fetch all the rows one time.

anakin
A: 

SELECT * FROM tablename WHERE ... LIMIT 5

how can i add page numbers of comments ?? 1 2 3 4 ...