2

I've been trying to get for examples rows 100 through 200 from a table of 1000+ rows. I found this snippet online that seems to work perfectly in PHPMyAdmin but when I try to use it in code, it won't work.

    SELECT * 
FROM ( 
    SELECT 
        @row := @row +1 AS rownum, id
    FROM ( 
        SELECT @row :=0) r, myGames
    ) ranked 
WHERE rownum >= 100 AND rownum < 200"

Here is my PHP Code

$q = "SELECT * 
FROM ( 
    SELECT 
        @row := @row +1 AS rownum, id
    FROM ( 
        SELECT @row :=0) r, myGames
    ) ranked 
WHERE rownum >= 100 AND rownum < 200";

$query = mysql_query($q);

When I try to do $query = mysql_query($q) or die(mysql_error()), I get nothing.

Any help with this is greatly appriciated

EDIT: SOLVED by using LIMIT 100,100. Thanks guys

4
  • Does any query from code work? Commented Nov 18, 2012 at 20:41
  • 1
    Does $query return a value? tried a var_dump($query) to check that it's actually failing? Commented Nov 18, 2012 at 20:42
  • 1
    Why not just use LIMIT 100,100? That'd give you the 100-200 result range, without the extra sub-queries... Commented Nov 18, 2012 at 20:43
  • mysql_*() functions are to no longer be used and will be removed in future releases. Please us MySQLi or PDO. Commented Nov 18, 2012 at 20:44

1 Answer 1

3

If you want to retrieve a subset of your result set, use limit offset, num_rows

select id
from myGames
limit 100, 100;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.