Hi again people.

I am currently looking to do a MySQL SELECT Query but I am not sure whether to enter the limit numbers are the end as show below:

$sql = "SELECT * FROM `customers` LIMIT 0, 30 ";

or should I just do:

$sql = "SELECT * FROM `customers`";

As you may have noticed I AM A NOOB 🙂 So please don't be hard on me, Im here to learn and have a great time.

    Well that depends. Do you want to limit your SELECT query to the first 30 results, or don't you?

      Ok. Thank you. So if I were to choose 5, 22. It would select data from rows 5, 22? Also, is there a way to make the limit arguments come from user input? So in other words make the 5 and 22 variables that can be changed in order to select specific data?

        I think LIMIT 5, 22
        will give you the result rows: 6-27

        LIMIT 0, 10 will give 1-10
        LIMIT 10, 10 will give 11-20
        LIMIT 20, 10 will give 21-30
        This is useful when displaying page 1, 2, 3 with 10 records per page

        Of course you may use variables in query with LIMIT
        $start = 20;
        $num = 10;
        "LIMIT $start, $num" ... will give 21-30

          RtK Evasion;10942321 wrote:

          Ok. Thank you. So if I were to choose 5, 22. It would select data from rows 5, 22?

          Common misconception. No, "LIMIT 5, 22" does not show you rows 5-22; it actually shows you rows 6-27 (or rows 5-26, if you consider the first row to be row #0 as MySQL does).

          The first parameter tells MySQL which row to start at, and the second parameter specifies how many rows you want. See this section in the MySQL manual for more information/examples.

          RtK Evasion;10942321 wrote:

          Also, is there a way to make the limit arguments come from user input? So in other words make the 5 and 22 variables that can be changed in order to select specific data?

          Well of course - they're just part of a SQL query which is nothing more than a string. Search the boards for "pagination" and you'll find plenty of examples showing to use page numbers and/or "Next/Previous" buttons to paginate data coming from a database.

            Write a Reply...