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.


1️⃣ Using mysqli (Procedural)


 

✅ Returns first 3 rows from the table.


2️⃣ Using mysqli Object-Oriented


 

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


3️⃣ Using PDO


 

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


4️⃣ 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...