Question:

SQL: Limiting results in PHP?

by  |  earlier

0 LIKES UnLike

Alright, I've been trying to work this query out for about an hour now and it's starting to p**s me off...so, after trying to figure it out on my own, I hope the YA experts can help me out...

I'm trying to do a simple select query using PHP...it goes something like this:

// In this instance, the variables are..

$from = 0;

$archive = 5;

// The query..

$query = "SELECT * FROM table WHERE field1 = 'value' AND field2 = 'value' ORDER BY field3 DESC LIMIT " . $from . ", " . $archive . "";

$content = $db->query($query);

I don't understand it, the query works fine if I remove the limit from the sql statement. However, I am trying to limit the results so I can put some paging in there...please help!!

 Tags:

   Report

1 ANSWERS


  1. Hey Chris,

    Check this out:

    $query = "SELECT * FROM table WHERE field1 = 'value' AND field2 = 'value' ORDER BY field3 DESC LIMIT " . $from . "," . $archive;

    See if that works, if not send me an email and I'll get it worked out for you.

    Later,

    Chad

    Edit: The LIMIT clause can be used to constrain the number of rows returned by the SELECT  statement. LIMIT takes one or two numeric arguments, which must both be non-negative integer constants (except when using prepared statements).

    With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):

    SELECT * FROM tbl LIMIT 5,10;  # Retrieve rows 6-15

    http://dev.mysql.com/doc/refman/5.0/en/s...

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.