限制mysql获取数组?

时间:2012-10-08 11:28:52

标签: mysql

有谁知道如何限制从MySql数组中提取的配置文件图像的数量来说明6个配置文件图像?

由于

代码:

   $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>";


    }
?>

3 个答案:

答案 0 :(得分:2)

将LIMIT子句添加到您的sql

ORDER BY `date_column` DESC LIMIT 6

答案 1 :(得分:0)

您可以对查询使用限制

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

或者您可以使用计数器

$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++;
    }

请注意,fetch_counter从0开始,所以如果它是6,则fetch已经运行了7次。

答案 2 :(得分:0)

设置sql查询的限制。像,

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