mySQL error "near ' 1' at line 1"

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
sathoro
Forum Newbie
Posts: 3
Joined: Sun Nov 28, 2010 1:49 pm

mySQL error "near ' 1' at line 1"

Post by sathoro »

I have the following code

Code: Select all

$result = mysql_query("SELECT * FROM wallpapers WHERE resolution = '$resolution' ORDER BY id DESC LIMIT $random, 1")
and it is getting the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 1' at line 1
I've tried putting $random in quotes, spacing it differently, etc. but I have no idea what is causing the error. Any ideas?

Thanks!
sathoro
Forum Newbie
Posts: 3
Joined: Sun Nov 28, 2010 1:49 pm

Re: mySQL error "near ' 1' at line 1"

Post by sathoro »

oh, and $random is from:

Code: Select all

$numbers = range(1, $j);
shuffle($numbers);
$random = $numbers[$c_rand];
sathoro
Forum Newbie
Posts: 3
Joined: Sun Nov 28, 2010 1:49 pm

Re: mySQL error "near ' 1' at line 1"

Post by sathoro »

fixed it! turns out it was from going out of the array's bounds. this fixed it:

Code: Select all

if ((count($numbers) - 1) >= $c_rand) {
	$random = $numbers[$c_rand]; // gets random number
}	
Post Reply