Right syntax to use near order by id desc limit 1 - php

If the code is executed, it shows the error:
check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY ID DESC limit 1'
Like in the following example:
$prevquery = "SELECT * FROM $tbl_name WHERE ID < $ID ORDER BY ID DESC limit 1";
$prevresult= mysql_query($prevquery) or die(mysql_error());
while($prevrow = mysql_fetch_row($prevresult))
{
displaying the previous ID:-
$prevID = $prevrow['ID'];
}
What should one do to prevent this?

Try this
$prevquery = "SELECT * FROM $tbl_name WHERE ID < '$ID' ORDER BY ID DESC limit 1";
If this does not work then echo the query and run in mysql phpmyadmin panel
echo $prevquery = "SELECT * FROM $tbl_name WHERE ID < '$ID' ORDER BY ID DESC limit 1";

This should work
$prevquery = "SELECT * FROM $tbl_name WHERE ID < '$ID' ORDER BY ID DESC limit 1";
$prevresult= mysql_query($prevquery) or die(mysql_error());
while($prevrow = mysql_fetch_row($prevresult))
{
displaying the previous ID:-
$prevID = $prevrow['ID'];
}

An additional note, whenever I see a generic error of the form...
check the manual that corresponds to your MySQL server version for the
right syntax to use near 'ORDER BY ID DESC limit 1'
The hint to look "near" almost always points to a problem in the sql statement just prior to where the single-quoted string shown in the error message begins.
So in this case the error quotes your sql string beginning at 'ORDER BY' which is immediately preceded by $ID. I would bet $ID is either not defined (and ends up being blank in your sql string), or $ID is not the correct data type.
Either way, as others have suggested you need to echo or log your sql string to see what is actually being queried.

You cant user 'where' with LIMIT. Instead use ORDER by id desc LIMIT 1

Related

Getting error while executing SQL query to get result search by keyword using PHP and MySQL

My requirement is when user will type letter inside text box at front end it will auto search from database and give the result accordingly. I have written some query but it gave me the following error.
Error: #1064 - 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 '%P%) ORDER BY member_id DESC LIMIT 0, 30' at line 1
My code is given below.
$searchKey=$_GET['searchKey'];
$keyword = '%'.$searchKey.'%';
$sql =mysqli_query($connect,"SELECT * FROM db_restaurant_basic WHERE rest_name LIKE (:keyword) ORDER BY member_id DESC ");
My first search keyword was p.
You want
$sql =mysqli_query($connect,"SELECT * FROM db_restaurant_basic WHERE rest_name LIKE '$keyword' ORDER BY member_id DESC ");
or also you could do
$sql =mysqli_query($connect,"SELECT * FROM db_restaurant_basic WHERE rest_name LIKE '" . $keyword . "' ORDER BY member_id DESC ");
(:keyword) is not going to pull in the variable for your keyword into the SQL syntax and also (:keyword) is not valid mysql
Another approach,
$sql =mysqli_query($connect,"SELECT * FROM db_restaurant_basic WHERE rest_name LIKE '".$keyword."' ORDER BY member_id DESC ");
It's better to use single quotes inside double quotes in relevant places when executing SQL queries.Sometimes you'll need to put single quotes for table name as well.Like this,
$sql =mysqli_query($connect,"SELECT * FROM `db_restaurant_basic` WHERE rest_name LIKE '".$keyword."' ORDER BY member_id DESC ");

php Error: Resourse id #4

for($i = 0;$i<10;$i++)
{
$query1 ="SELECT `id` FROM radcheck ORDER BY `id` DESC LIMIT 1;";
$lololo= mysql_query($query1) or die(mysql_error());
//echo $lololo;
$query = "INSERT INTO radcheck (username,attribute,op,value) VALUES ('teleuser".$lololo."','Cleartext-Password',':=','$arr1[$i]')";
mysql_query($query) or die(mysql_error());
}
I am trying to retrieve my latest id value to append with my username. And the value retrieve always be 'Resource id #4'.
Is there anyway to solve it ?
Thank you.
Mysql doesn't support TOP that is for SQL Server.
Instead of using TOP you can use mysql LIMIT so your query would be:
SELECT `id` FROM radcheck ORDER BY `id` DESC LIMIT 1;
You do not have to use single quotes around column names. if you need to escape it use backticks. And TOP is not mysql syntax. You have to use limit
SELECT `id` FROM radcheck ORDER BY `id` DESC limit 1
Do not longer use the depricated mysql_* API. Use mysqli_* or PDO
Remove the 's around field names -
"SELECT TOP 1 id FROM radcheck ORDER BY id DESC";
But as Daan told it will not work for mysql, then ORDER & LIMIT will do the trick.
"SELECT id FROM radcheck ORDER BY id DESC LIMIT 1";
i hope you never mind trying this pattern
Select id from radcheck WHERE id=1 ORDER BY 'id' DESC ;

MySQL,PHP: ORDER BY shoots an error.

Whenever I try to use ORDER BY I'm getting an error saying that it's something bad with syntax
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 'ORDER BY id' at line 1
For me, the syntax looks perfectly good, everything works as long as I don't use ORDER BY id. This is how it works:
$sql = "SELECT * FROM lessons $limit";
This is how I want it to work:
$sql = "SELECT * FROM lessons $limit ORDER BY id";
I have also tried this, but with no luck:
$sql = "SELECT * FROM lessons $limit ORDER BY id DESC";
ORDER BY needs to go before LIMIT.
Try:
$sql = "SELECT * FROM lessons ORDER BY id $limit";

adding PHP variable to SQL statement. Not working

I can't get a variable to work in SQL statement. I can get it to work when I replace (username = $user) with (ID = 11) which is another column from database and a specific row (11), but I want to include a specific row matching $user from column 'username', along with other random results with a limit of $sn.
When using var_dump($user) I know that the variable has a value, but can't see why it doesn't work in SQL statement.
$photo=mysql_query("SELECT A. * FROM (
SELECT DISTINCT * FROM profile_images
WHERE approved='N'
ORDER BY (username = $user) DESC, RAND()
LIMIT $sn)
as A ORDER BY RAND()");
Getting error message: 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 '#googlemail.com) DESC, RAND() LIMIT 9) as A ORDER BY RAND()' at line 4
Any help appreciated.
Assuming $sn holds integer value and don't require escaping,
$photo=mysql_query("SELECT A. * FROM (
SELECT DISTINCT * FROM profile_images
WHERE approved='N'
ORDER BY (username = '".mysql_real_escape_string($user)."') DESC, RAND()
LIMIT $sn)
as A ORDER BY RAND()");
In general, consider using PDO and bind parameters.

MySQL Where Clause error

i have the following statement
$result = mysql_query("SELECT * from rests ORDER BY name asc WHERE flag = '1' LIMIT 0 , 20");
am getting the following error
Invalid query: 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 'WHERE flag = '1' LIMIT 0 , 20' at line 1
am not sure where am going wrong :(
$result = mysql_query("SELECT * from rests WHERE flag = '1' ORDER BY name asc LIMIT 0 , 20");
You cannot have the ORDER BY clause before WHERE clause.
Refer to the MySQL select syntax for the correct order of various clauses.
Try this:
SELECT * from rests WHERE flag = '1' ORDER BY name asc LIMIT 0 , 20
Also Ordering in ascending is default, you may drop the asc.
The ORDER BY clause must be after the WHERE clause. That's all it is.
ORDER BY comes after the WHERE and LIMIT at the end.
WHERE goes before ORDER BY.

Categories