PHP MySQL Using the LIMIT Clause

PHP Tutorial

PHP MySQL Using the LIMIT Clause

In PHP MySQL Using the LIMIT Clause is used to restrict the number of records returned in a SELECT query. It’s very useful for pagination or displaying a subset of data.


 Using mysqli (Procedural)


 

  •  Returns first 3 rows from the table.

 Using mysqli Object-Oriented


 

  • LIMIT offset, count → skip offset rows and return count rows.

 Using PDO


 

  •  In PDO, bind parameters as PDO::PARAM_INT when using LIMIT.

 Key Points

  1. LIMIT restricts the number of rows returned.

  2. Syntax:

    • LIMIT count → returns the first count rows

    • LIMIT offset, count → skip offset rows, return count rows

  3. Useful for pagination, e.g., showing 10 records per page.

  4. Works with mysqli procedural, object-oriented, and PDO.

  5. Always bind LIMIT parameters as integers in PDO to avoid errors.

You may also like...