Mark Tanner
Mark Tanner

Reputation: 1

limit mysql fetch array?

Does anyone know how I could limit the number of profile images that are fetched from MySql array to say 6 profile images?

Thanks

Code:

   $newest_set = get_newest_profile() ;

    while ($newest = mysql_fetch_array($newest_set )){
        echo" 
        <div class=\"mod_newest_image\">
        <a href=\"profile.php?id={$newest['id']}\"><img width=95px src=\"data/photos/{$newest['id']}/_default.jpg\" /></a>
        <div>
        <strong> {$newest['display_name']}</strong>
        <br />
        </div>
        </div>";


    }
?>

Upvotes: 0

Views: 2834

Answers (3)

Sudip
Sudip

Reputation: 2051

Set a limit on your sql query. Like,

$sql=mysql_query("select fields from tablename where conditions limit 0,6");

Upvotes: 0

Mathlight
Mathlight

Reputation: 6653

You could use an limit to your query

 SELECT * FROM `your_table` LIMIT 0, 6 // takes the rows from the results 0 - 6

Or you can use an counter

$fetch_counter = 0;
while ($newest = mysql_fetch_array($newest_set ) && $fetch_counter < 6){
        echo" 
        <div class=\"mod_newest_image\">
        <a href=\"profile.php?id={$newest['id']}\"><img width=95px src=\"data/photos/{$newest['id']}/_default.jpg\" /></a>
        <div>
        <strong> {$newest['display_name']}</strong>
        <br />
        </div>
        </div>";
        $fetch_counter++;
    }

Note that fetch_counter starts at 0, so if it is 6, the fetch is runned 7 times already.

Upvotes: 0

xdazz
xdazz

Reputation: 160853

Add LIMIT clause to your sql

ORDER BY `date_column` DESC LIMIT 6

Upvotes: 2

Related Questions