简体   繁体   中英

How to limit number of entries from MySQL database?

Im trying to get some values from mysql and echo each value inside an article. Everything is working fine , I just wanna know how to limit page articles to 15 per page and when the limit is reached create page #2 etc , Here is the php code im using

$query = "SELECT `text` FROM `items` ORDER BY 'id'"; 
if ($query_run = mysql_query($query))     

    while ($query_row = mysql_fetch_assoc($query_run)) {
        $text = $query_row['text'];     
        echo "<article>$text</article>";
    }
} else {
    echo mysql_error($conn_error);
}

Any suggestions? Thanks .

This has nothing to do with HTML. Add a LIMIT clause to your SQL query.

select `text` from `items` order by 'id' limit 15

Read up on SQL Pagination .

In addition to @meagar's answer: mysql is deprecated. You should use the mysqli improved API.

Example:

$mysqli = new mysqli('host', 'user', 'password', 'databasename');
$query = "SELECT `text` FROM `items` ORDER BY 'id' LIMIT 15";


if($result = $mysqli->query($query)){

    while ($row = $result->fetch_assoc()) {
        $text = $row['text'];     
        echo "<article>$text</article>";
    }

}         

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM