Omar IW
Omar IW

Reputation: 11

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 .

Upvotes: 0

Views: 821

Answers (2)

hammus
hammus

Reputation: 2612

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

}         

}

Upvotes: 1

user229044
user229044

Reputation: 239301

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.

Upvotes: 1

Related Questions