Error using LIMIT in MySQL

Reply

Join Date: Nov 2008
Posts: 99
Reputation: csharplearner is an unknown quantity at this point 
Solved Threads: 3
csharplearner's Avatar
csharplearner csharplearner is offline Offline
Junior Poster in Training

Error using LIMIT in MySQL

 
0
  #1
May 30th, 2009
Hi
I trying to implement pagination using LIMIT in MySQL. My problem is it works good for the first 3 results only and for later results it shows error saying check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 3, 3' at line 1

I couldnt get where i am going wrong here. Please look at my code below:
  1. if($_GET["c"])
  2. { $k = $_GET["k"] ; $cnt = $k; $k = $k + 3; }
  3. else
  4. {$cnt = 0; $k = 3;}
  5.  
  6. $table = '<table >';
  7.  
  8. mysql_connect(" ") ;
  9. mysql_select_db(" ");
  10. $result = mysql_query("SELECT var1 FROM $tab1 LIMIT $cnt, 3") or die(mysql_error());
  11. $i = 0;
  12. while($row = mysql_fetch_array( $result ))
  13. {
  14. $var1 = $row['name'];
  15. if ( $i == 3 ) {
  16. $table .= '</tr><tr>';
  17. $i = 0;
  18. }
  19.  
  20. mysql_connect(" ") ;
  21. mysql_select_db(" ");
  22. $result1 = mysql_query("SELECT * FROM tab2 WHERE var2= '$var1'");
  23. $row = mysql_fetch_array( $result1 );
  24. $var2 = $row['var2'];
  25. $table .= "<td > blah blah blah";
  26.  
  27. $i++;
  28. }
  29.  
  30. $table .= "<tr><td ><a href=link.php?c=next&k=".$k.">next</a></td></tr>";
  31. echo $table;

All the variables are retreived properly the only problem seems to be somewhere with LIMIT statement. Please let me know where am i going wrong.

Thank you in advance.
Last edited by csharplearner; May 30th, 2009 at 11:49 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 608
Reputation: OmniX is an unknown quantity at this point 
Solved Threads: 8
OmniX's Avatar
OmniX OmniX is offline Offline
Practically a Master Poster

Re: Error using LIMIT in MySQL

 
1
  #2
May 31st, 2009
You stated your sql statement is LIMIT 3, 3.
That line of code will only gather 3 results try changing it to LIMIT 3, 4 and see if that works?
"You never stop learning." - OmniX
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,429
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 132
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso

Re: Error using LIMIT in MySQL

 
1
  #3
May 31st, 2009
There are a few bugs and security holes in that script. So try the following:
  1. mysql_connect(" ") ; //add variables in function
  2. mysql_select_db(" "); //add variables in function
  3.  
  4. if(!empty($_GET["c"]) && !preg_match('/[^0-9]/',$_GET['k']))
  5. { $k = $_GET["k"] ; $cnt = $k; $k += 3; }
  6. else
  7. {$cnt = 0; $k = 3;}
  8.  
  9. $table = '<table >';
  10.  
  11.  
  12. $result = mysql_query('SELECT var1 FROM '.mysql_real_escape_string($tab1).' LIMIT '.mysql_real_escape_string($cnt).', 3') or die(mysql_error());
  13.  
  14. //for loops are faster than while loops when used properly.
  15. for ($i=0;$row = mysql_fetch_assoc( $result );$i++)
  16. {
  17. $var1 = $row['name'];
  18. if ( $i == 3 ) {
  19. $table .= '</tr><tr>';
  20. $i = 0;
  21. }
  22.  
  23.  
  24. $result1 = mysql_query('SELECT * FROM tab2 WHERE var2= "'.mysql_real_escape_string($var1).'"');
  25. $row = mysql_fetch_assoc( $result1 );
  26. $var2 = $row['var2'];
  27. $table .= "<td > blah blah blah";
  28.  
  29. }
  30.  
  31. $table .= "<tr><td ><a href=link.php?c=next&k=".$k.">next</a></td></tr>";
  32. echo $table;
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 99
Reputation: csharplearner is an unknown quantity at this point 
Solved Threads: 3
csharplearner's Avatar
csharplearner csharplearner is offline Offline
Junior Poster in Training

Re: Error using LIMIT in MySQL

 
0
  #4
May 31st, 2009
Originally Posted by OmniX View Post
You stated your sql statement is LIMIT 3, 3.
That line of code will only gather 3 results try changing it to LIMIT 3, 4 and see if that works?
Thank you for your time and reply.
I couldnt understand why should i change it to '4' because after LIMIT
the first variable is offset and next one is number of records we want to display right?! Then cant i put whatever number of records i want changing the offset value.
Please explain if i am not following.
Thank you.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 99
Reputation: csharplearner is an unknown quantity at this point 
Solved Threads: 3
csharplearner's Avatar
csharplearner csharplearner is offline Offline
Junior Poster in Training

Re: Error using LIMIT in MySQL

 
0
  #5
May 31st, 2009
Originally Posted by OmniX View Post
You stated your sql statement is LIMIT 3, 3.
That line of code will only gather 3 results try changing it to LIMIT 3, 4 and see if that works?
Thank you for your time and reply.
I couldnt understand why should i change it to '4' because after LIMIT
the first variable is offset and next one is number of records we want to display right?! Then cant i put whatever number of records i want changing the offset value.
Please explain if i am not following.
Thank you.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 340
Reputation: Josh Connerty is an unknown quantity at this point 
Solved Threads: 26
Josh Connerty's Avatar
Josh Connerty Josh Connerty is offline Offline
Posting Whiz

Re: Error using LIMIT in MySQL

 
0
  #6
May 31st, 2009
I always thought the first was the start recoed and the second was the last record.

I think 3,3 is wrong run an if statement to make sure the count variable is not equal to or greater to 3 this would confuse MySql's brain.
Posts should be like mini-skirts, long enough to cover enough, but not too long that you cover too much.

My Liveperson: http://liveperson.com/josh-connerty/
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,429
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 132
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso

Re: Error using LIMIT in MySQL

 
1
  #7
Jun 1st, 2009
Originally Posted by Josh Connerty View Post
I always thought the first was the start recoed and the second was the last record.

I think 3,3 is wrong run an if statement to make sure the count variable is not equal to or greater to 3 this would confuse MySql's brain.
Below is a direct quote from the select syntax of the official mysql manual:
  1. LIMIT {[offset,] row_count | row_count OFFSET offset}
As you can see, it makes an if statement. It can be broken down to two parts. The first is:
  1. LIMIT {row_count}
The above statement make just the row count and note that when something is in brackets it is an optional parameter optional. However, if you were to fill in the optional parameter, since there are now two parameters, the if statement would change to the following where the offset is at the end:
  1. LIMIT {row_count OFFSET offset}
And now for the part that is confusing most people in this thread. An offset is never relative to the row_count. So say you wanted to limit from rows 6 to row 8 you would use LIMIT 6, 2 as you are telling mysql how many rows to count on after six. That's what offset means in any language weather it's mysql, graphics, english etc.

So to explain what LIMIT 3, 3 does. It will limit from result 3 to result 6 as it has counted three rows from result 3. I would have to say that if anything was the error it would be the table name.
As a test, try replacing line 12 of my previously posted script with the following and see what query it reports back:
  1. $result = mysql_query('SELECT var1 FROM `'.mysql_real_escape_string($tab1).'` LIMIT '.mysql_real_escape_string($cnt).', 3')
  2. or die('SELECT var1 FROM `'.mysql_real_escape_string($tab1).'` LIMIT '.mysql_real_escape_string($cnt).', 3<hr>'.mysql_error());
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC