returning limited results in mysql

Busy's picture

He has: 6,151 posts

Joined: May 2001

this works

<?php
$result
= mysql_query(\"SELECT pid, phits FROM jokes order by phits desc\");
?>

and this works
<?php
$result
= mysql_query(\"SELECT pid, phits FROM jokes limit 0,30\");
?>

but this doesn't
<?php
$result
= mysql_query(\"SELECT pid, phits FROM jokes order by phits desc limit 0,30\");
?>

any ideas? I only want to display 30 results but they have to be in desc order.

He has: 1,016 posts

Joined: May 2002

I don't see any problems with your code. It should work fine. Try putting echo mysql_error(); after to see what the problem is.

Busy's picture

He has: 6,151 posts

Joined: May 2001

causes a Mysqld-opt illegal error
I use the mysql_error() function
code goes:

<?php
$result
= mysql_query(\"SELECT pid, phits FROM jokes order by phits desc limit 0,30\");
if(!
$result) {
    echo (\"<p>Error performing task: \". mysql_error() . \"</p>\");
  exit();
      } else {
....
?>

He has: 1,016 posts

Joined: May 2002

Just do..

<?php
$result
= mysql_query(\"SELECT pid, phits FROM jokes order by phits desc limit 0,30\");
echo mysql_error();
exit;
?>

Busy's picture

He has: 6,151 posts

Joined: May 2001

is it exit or exit() ?

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Doesn't matter... a quote from the user comments:

Quote: Since exit is a language construct and not a function, doing:

exit;

without the parenthesis is legal.

Mark Hensler
If there is no answer on Google, then there is no question.

Busy's picture

He has: 6,151 posts

Joined: May 2001

same thing, crashs mysql, heres the illegal op details if you can undertsand them:

MYSQLD-OPT caused an invalid page fault in
module MYSQLD-OPT.EXE at 018f:0045dbee.
Registers:
EAX=00000000 CS=018f EIP=0045dbee EFLGS=00010287
EBX=0382fb73 SS=0197 ESP=0179f73c EBP=69e28001
ECX=00000001 DS=0197 ESI=00a04100 FS=83ef
EDX=00a91228 ES=0197 EDI=00000000 GS=0000
Bytes at CS:EIP:
39 11 75 3b 33 d2 8a 51 14 f6 d2 33 db 81 e2 ff
Stack dump:
705f6e6d 00000005 00a94cc0 0382fb73 0045da27 00a04100 00a94cc0 00000005 00000000 00a042f0 014457c0 00a04100 00000000 0045dc61 00a042f0 00a04100

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Are you running mysqld on a windows box?

Busy's picture

He has: 6,151 posts

Joined: May 2001

yep win98 se, apart from this one thing havent had any other problems

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Alright, I had a similar problem running mysql on a Win box. It's been a few years now. Some queries would crash mysql on my win box, but work perfectly once I uploaded them to a unix box. Very odd.

Check to make sure you have the latest version. If that doesn't fix it, you'll probably have to keep the ORDER BY clause and fake the LIMIT clause using PHP and a loop structure.

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 447 posts

Joined: Oct 1999

if installing the latest version doesnt help, i'd say join the devlist and see if you cant get some answers there.

Busy's picture

He has: 6,151 posts

Joined: May 2001

Thanks everyone, I'll try update it,
I test on windows but upload to unix, It's not an important page so if upgrading doesnt help might cheat it like Mark mentioned

*maybe I should just get rid of windows :D*

Busy's picture

He has: 6,151 posts

Joined: May 2001

Thanks Mark, I upgraded MYSQL and now it works Laughing out loud

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.