Jump to content

Limit results of table output by using ?limit=10


Chendyboi

Recommended Posts

I am trying to limit my table results to 10 when my url is /table.php?limit=10

 

the code that i am using is as follows:

 

    if(!isset($_GET['limit']) && $_GET['limit'] == '10') {
    $fetch = mysql_query("SELECT * FROM (table name) LIMIT 0, 10")or
    die(mysql_error());
    }

 

Could someone help me on where i am going wrong.

Link to comment
Share on other sites

Your code says:

If the limit is not set, and the limit is equal to 10 (a condition that can never happen),

then fetch ten rows.

 

Your code SHOULD SAY:

if the limit is set

cast the limit to an integer, then fetch that many rows.

Link to comment
Share on other sites

What Jessica said is do something like this:

//Note: removed ! in the line below
    if(isset($_GET['limit'])) {
   $num_rows=intval($_GET['limit'] ) ; //cast to an integer - makes it work for any number of rows
    $fetch = mysql_query("SELECT * FROM (table name) LIMIT 0, $num_rows")or
    die(mysql_error());
    }

also needed to remove the test for if limit was 10

Edited by davidannis
Link to comment
Share on other sites

mikosiko is right. You need to post code. Without seeing any code my best guesses are that you need an actual table name where you have (table name) in your select, you don't have a valid connection to the database, or you are not retrieving and displaying the results after the mysql_query.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.