Financial Calculator

Calculate loans, SIP, PPF, NPS & more!

Financial Calculator

Calculate loans, SIP, PPF, NPS & more!

MySQL Limit Data

PHP

MySQL Limit Data

LIMIT clause MySQL में records की संख्या को control करने के लिए उपयोग किया जाता है। PHP में LIMIT का उपयोग डेटा को छोटे भागों में fetch करने, pagination बनाने, load कम करने और performance बढ़ाने के लिए किया जाता है। यह clause database queries को efficient बनाता है, खासकर जब table में हजारों records हों।

  • LIMIT X → पहले X records return करेगा।
  • LIMIT X, Y → X records skip करेगा और अगले Y records return करेगा (pagination में उपयोगी)।
  • LIMIT का उपयोग हमेशा SELECT query के साथ होता है।

LIMIT कब उपयोग किया जाता है?

  • Pagination (जैसे: Page 1, Page 2, Page 3)
  • Preview data (top 5, top 10 records)
  • Random user listing में कुछ ही users दिखाना
  • Server load कम करने के लिए

LIMIT का Basic Syntax

Basic SQL: LIMIT
SELECT * FROM students LIMIT 5;
-- पहले 5 records

SELECT * FROM students LIMIT 5, 10;
-- पहले 5 skip → अगले 10 records

Important Notes

  • Note: LIMIT के साथ हमेशा ORDER BY उपयोग करें ताकि results हर बार एक ही pattern में आएँ।
  • Warning: बिना ORDER BY के LIMIT random results दे सकता है (खासकर production DB में)।
  • Pagination के लिए LIMIT start, count वाला तरीका सबसे ज्यादा used है।

MySQLi Procedural Example (LIMIT)

यह example students टेबल के पहले 5 records fetch करता है:

उदाहरण: MySQLi Procedural (LIMIT)
<?php
$conn = mysqli_connect("localhost", "root", "", "school");

$sql = "SELECT * FROM students ORDER BY id ASC LIMIT 5";
$result = mysqli_query($conn, $sql);

while ($row = mysqli_fetch_assoc($result)) {
  echo $row['name'] . " - Class: " . $row['class'] . "<br>";
}

mysqli_close($conn);
?>

Pagination Example (MySQLi)

यह example pagination (page → 1,2,3...) बनाने में उपयोग होता है।

उदाहरण: Pagination (MySQLi LIMIT)
  $limit = 10; // प्रति पेज 10 records
  $page = 2; // Page 2
  $start = ($page - 1) * $limit;

  $sql = "SELECT * FROM students ORDER BY id ASC LIMIT $start, $limit";

PDO Example (LIMIT)

PDO में LIMIT use करना बहुत आसान है:

उदाहरण: PDO (LIMIT)
  <?php
  $pdo = new PDO("mysql:host=localhost;dbname=school", "root", "");

  $stmt = $pdo->query("SELECT * FROM students ORDER BY id ASC LIMIT 5");
  $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

  foreach ($rows as $row) {
    echo $row['name'] . " - Class: " . $row['class'] . "<br>";
  }
  ?>

PDO Pagination Example

उदाहरण: PDO Pagination
  $limit = 10;
  $page = 3;
  $start = ($page - 1) * $limit;

  $stmt = $pdo->prepare("SELECT * FROM students ORDER BY id ASC LIMIT :start, :limit");
  $stmt->bindValue(':start', $start, PDO::PARAM_INT);
  $stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
  $stmt->execute();

Best Practices for LIMIT

  • Always use ORDER BY ताकि results stable रहें।
  • Pagination में LIMIT offset method उपयोग करें।
  • Large tables के लिए LIMIT slow हो सकता है → INDEX create करें।
  • API responses में हमेशा LIMIT का उपयोग करें (overload रोकने के लिए)।

Common Mistakes (and Fixes)

  • गलती 1: OFFSET को गलत calculate करना → हमेशा (page-1)*limit formula लागू करें।
  • गलती 2: LIMIT के बिना सारा data load कर लेना → Slow + heavy load।
  • गलती 3: ORDER BY न लगाना → हर बार different results।

Conclusion

LIMIT MySQL का एक powerful clause है जो performance optimization, user experience (pagination) और resource management के लिए आवश्यक है। PHP में इसे MySQLi और PDO दोनों में आसानी से उपयोग किया जा सकता है। हमेशा ORDER BY और LIMIT साथ में उपयोग करें ताकि results consistent मिलें।

Article By: Brajlal Prasad
Created on: 05 Nov 2025  13  Views
 Print Article
Report Error

If you want to report an error, or any suggestion please send us an email to [email protected]

Financial Calculator

Financial Calculator

Take control of your finances with our powerful Financial Calculator app—calculate loans, SIP, PPF, NPS and more all in one place!

Play Store