943,765 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1229
  • PHP RSS
May 30th, 2009
0

Error using LIMIT in MySQL

Expand Post »
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:
php Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 11
Solved Threads: 3
Junior Poster in Training
csharplearner is offline Offline
99 posts
since Nov 2008
May 31st, 2009
1

Re: Error using LIMIT in MySQL

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?
Reputation Points: 31
Solved Threads: 10
Practically a Master Poster
OmniX is offline Offline
652 posts
since Dec 2007
May 31st, 2009
1

Re: Error using LIMIT in MySQL

There are a few bugs and security holes in that script. So try the following:
php Syntax (Toggle Plain Text)
  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;
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
May 31st, 2009
0

Re: Error using LIMIT in MySQL

Click to Expand / Collapse  Quote originally posted by OmniX ...
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.
Reputation Points: 11
Solved Threads: 3
Junior Poster in Training
csharplearner is offline Offline
99 posts
since Nov 2008
May 31st, 2009
0

Re: Error using LIMIT in MySQL

Click to Expand / Collapse  Quote originally posted by OmniX ...
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.
Reputation Points: 11
Solved Threads: 3
Junior Poster in Training
csharplearner is offline Offline
99 posts
since Nov 2008
May 31st, 2009
0

Re: Error using LIMIT in MySQL

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.
Reputation Points: 31
Solved Threads: 27
Unverified User
Josh Connerty is offline Offline
342 posts
since Apr 2009
Jun 1st, 2009
1

Re: Error using LIMIT in MySQL

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:
PHP Syntax (Toggle Plain Text)
  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:
PHP Syntax (Toggle Plain Text)
  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:
PHP Syntax (Toggle Plain Text)
  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:
php Syntax (Toggle Plain Text)
  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());
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: can we achieve 3-tier architecture with out frameworks in php?
Next Thread in PHP Forum Timeline: Trouble grasping OOP concepts





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC