PHP MySQL Using the LIMIT Clause

⏱️ PHP MySQL – Using the LIMIT Clause

The LIMIT clause in MySQL 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.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *