Using 'to' in a PHP SQL query - php

My SQL query is SELECT * FROM chat WHERE to = '$user_id' AND client_id = '001' LIMIT 4
For some reason that query gives me 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 'to = '1' AND client_id = '001' LIMIT 4' at line 1
I used a different row and the query ran perfectly fine - is the error because of the word "to"? Or is there something else behind this?
Just for your reference, here's the PHP:
$user_id = $_SESSION['user_id'];
$client_id = '001';
if (!$query = sql("SELECT * FROM arrowchat WHERE to = '$user_id' AND client_id = '$client_id' LIMIT 4")) {
echo mysql_error();
} else {
echo 'success';
}

http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html
to is a reserved keyword in mysql, you cannot use it as is in a query. You need to wrap it in backticks:
SELECT * FROM chat WHERE `to` = '$user_id' AND client_id = '001' LIMIT 4

to is reserved mysql keyword you need to use backticks like that :
SELECT * FROM arrowchat WHERE `to` ....
reserved keywords

Related

How Do I Concatenate PHP variable in SQL Query Then Concatenate Additional SQL Queries

How Do I Concatenate PHP variable in SQL Query Then Concatenate Additional SQL Queries
$query = 'SELECT * FROM news_posts WHERE status ="approved" AND user_id='.$user_id.'ORDER BY news_id DESC';
I got an error:
check the manual that corresponds to your MariaDB server version for the right syntax to use near 'BY news_id DESC' at line 132
$query = 'SELECT * FROM news_posts WHERE status ="approved" AND
user_id='.$user_id.' ORDER BY news_id DESC';
try add a space before "ORDER"

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 ");

Right syntax to use near order by id desc limit 1

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

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.

Sql syntax error

Here is my query:
$query="Delete b
Where Exists
(
Select 1
From a
Where a.poster_password = '$pass'
And a.ad_id = '$id'
And a.classified_id = b.classified_id
)
Delete a
Where a.poster_password = '$pass'
And a.ad_id = '$id'";
I get this 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 'Where Exists ( Select 1 From a Where a.poster_p' at line 2
If you need more input let me know...
Whats wrong here?
Thanks
UDPATE:
Just a Q: Do I need to specify also that a = "this table" and b = "another table" or does MySql get that by this code?
As for the new code posted where to use FROM and a terminator semicolon, wont work and give this 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 'Delete FROM a Where a.poster_password = 'xxxxxxxxxxxxxxxxxxxxx' at line 10
UPDATE2:
$query="Delete FROM $sql_table
Where Exists
(
Select 1
From classified
Where classified.poster_password = '$pass'
And classified.ad_id = '$id'
And classified.classified_id = $sql_table.classified_id
);
Delete FROM classified
Where classified.poster_password = '$pass'
And classified.ad_id = '$id'";
And when I echo $query: (fordon is in this case $sql_table variable.)
Delete FROM fordon
Where Exists
(
Select 1
From classified
Where classified.poster_password = 'xxxxx'
And classified.ad_id = 'motorbat_166250627'
And classified.classified_id = fordon.classified_id
);
Delete FROM classified
Where classified.poster_password = 'xxxxx'
And classified.ad_id = 'motorbat_166250627'
Thanks again
You're not specifying the tables to delete from. Try:
$query="Delete FROM b
Where Exists
(
Select 1
From a
Where a.poster_password = '$pass'
And a.ad_id = '$id'
And a.classified_id = b.classified_id
);
Delete FROM a
Where a.poster_password = '$pass'
And a.ad_id = '$id'";
I've also added in a semicolon after the end of the first DELETE query. If you want to run both at the same time, you'll need a separator to terminate the first query, before you run the second version.
Re. your question edit about MySQL "getting" the tables - if a and b are aliases here, then no, MySQL doesn't know what a and b are. You'll need to alias the tables, or replace a and b with the actual table names.
The two deletes need to be separate statements ( and executed separately ).

Categories