| | |
Error using LIMIT in MySQL
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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:
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.
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)
if($_GET["c"]) { $k = $_GET["k"] ; $cnt = $k; $k = $k + 3; } else {$cnt = 0; $k = 3;} $table = '<table >'; mysql_connect(" ") ; mysql_select_db(" "); $result = mysql_query("SELECT var1 FROM $tab1 LIMIT $cnt, 3") or die(mysql_error()); $i = 0; while($row = mysql_fetch_array( $result )) { $var1 = $row['name']; if ( $i == 3 ) { $table .= '</tr><tr>'; $i = 0; } mysql_connect(" ") ; mysql_select_db(" "); $result1 = mysql_query("SELECT * FROM tab2 WHERE var2= '$var1'"); $row = mysql_fetch_array( $result1 ); $var2 = $row['var2']; $table .= "<td > blah blah blah"; $i++; } $table .= "<tr><td ><a href=link.php?c=next&k=".$k.">next</a></td></tr>"; 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.
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?
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
There are a few bugs and security holes in that script. So try the following:
php Syntax (Toggle Plain Text)
mysql_connect(" ") ; //add variables in function mysql_select_db(" "); //add variables in function if(!empty($_GET["c"]) && !preg_match('/[^0-9]/',$_GET['k'])) { $k = $_GET["k"] ; $cnt = $k; $k += 3; } else {$cnt = 0; $k = 3;} $table = '<table >'; $result = mysql_query('SELECT var1 FROM '.mysql_real_escape_string($tab1).' LIMIT '.mysql_real_escape_string($cnt).', 3') or die(mysql_error()); //for loops are faster than while loops when used properly. for ($i=0;$row = mysql_fetch_assoc( $result );$i++) { $var1 = $row['name']; if ( $i == 3 ) { $table .= '</tr><tr>'; $i = 0; } $result1 = mysql_query('SELECT * FROM tab2 WHERE var2= "'.mysql_real_escape_string($var1).'"'); $row = mysql_fetch_assoc( $result1 ); $var2 = $row['var2']; $table .= "<td > blah blah blah"; } $table .= "<tr><td ><a href=link.php?c=next&k=".$k.">next</a></td></tr>"; 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
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
•
•
•
•
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?
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.
•
•
•
•
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?
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.
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.
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/
My Liveperson: http://liveperson.com/josh-connerty/
•
•
•
•
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.
PHP Syntax (Toggle Plain Text)
LIMIT {[offset,] row_count | row_count OFFSET offset}
PHP Syntax (Toggle Plain Text)
LIMIT {row_count}
PHP Syntax (Toggle Plain Text)
LIMIT {row_count OFFSET offset}
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)
$result = mysql_query('SELECT var1 FROM `'.mysql_real_escape_string($tab1).'` LIMIT '.mysql_real_escape_string($cnt).', 3') 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
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
![]() |
Similar Threads
- The "LIMIT" parameter in queries (PHP)
- MySQL syntax error 1064 (MySQL)
- error in connecting MySql and PHPBB help me..... (MySQL)
- help on error handling in mysql ,in primary key (MySQL)
- About LIMIT in mysql (PHP)
- Error message while importing data to Excel from the MySQL db (MySQL)
- Terrible Error while using MySQL resources (PHP)
- Invision Power Board mysql error when trying to view newly created forums (PHP)
Other Threads in the PHP Forum
- Previous Thread: can we achieve 3-tier architecture with out frameworks in php?
- Next Thread: Trouble grasping OOP concepts
| Thread Tools | Search this Thread |
apache api array beginner binary body broken buttons cakephp checkbox class cms code cron curl database date date/time display dynamic ebooks echo email error file files folder form forms function functions global google href htaccess html image include insert ip javascript joomla limit link list login mail mediawiki menu mlm msqli_multi_query multiple mycodeisbad mysql number oop parameter paypal pdf php phpincludeissue problem query radio random recourse recursion regex remote script search seo server sessions sms source sp space speed sql static subdomain syntax system table tag tutorial update upload url validator variable vbulletin video web webdesign white wordpress xml youtube






