Jump to content

[SOLVED] MySQL LIMIT error


salman_ahad@yahoo.com

Recommended Posts

I am still getting error..

$Var=$_POST['limit'];
$VarCount = mysql_real_escape_string($Var);
$query = mysql_query("SELECT * FROM test WHERE id NOT IN (SELECT id FROM test1) AND LIMIT $VarCount ORDER BY datetime DESC ") or die(mysql_error());

//error
//You have an error in your SQL syntax;...check near LIMIT...etc

 

If I replace $VarCount(user defined) by 2,3,4(any integer).....it is working. Any ideas?

 

 

Link to comment
https://forums.phpfreaks.com/topic/179905-solved-mysql-limit-error/
Share on other sites

I'm a fan of using quotes...

 

$query = mysql_query("SELECT * FROM test WHERE id NOT IN (SELECT id FROM test1) AND LIMIT " . $VarCount . " ORDER BY datetime DESC ") or die(mysql_error());

 

And the people who have posted below are absolutely correct.  Limit needs to be at the end....

set your sql to a variable and then echo it to make sure it looks like it should.

$VarCount=mysql_real_escape_string($_POST['limit']); 
$sql="SELECT * FROM test WHERE id NOT IN (SELECT id FROM test1) AND LIMIT $VarCount ORDER BY datetime DESC "
echo $sql;
$query=mysql_query($sql); 

Only Taqui and Mike worked...

 

if(isset($_POST['Submit']))
{
$VarCount=mysql_real_escape_string($_POST['limit']);
$query = mysql_query("SELECT * FROM test WHERE id NOT IN (SELECT id FROM test1) ORDER BY datetime DESC LIMIT $VarCount") or die(mysql_error());

 

Thanks

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.