PHP ERROR in SQL syntax - php

It shows me:
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 company='ABC' AND branch='26' AND owner IS NULL' at line 1
$sql="SELECT * FROM spr ORDER BY id WHERE company='$_SESSION[company]' AND branch='$_SESSION[branch]' AND owner IS NULL";
I can't see what is going wrong with my query. Someboby help please...

The order by clause must come after the where clause.

Try this, it's the ORDER BY clause which should be at the end of the statement
$sql="SELECT * FROM spr
WHERE company='".$_SESSION[company]."'
AND branch='".$_SESSION[branch]."'
AND owner IS NULL ORDER BY id";

$sql="SELECT * FROM spr WHERE company='$_SESSION[company]' AND branch='$_SESSION[branch]' AND owner IS NULL ORDER BY id";

You need braces {} around array values and object properties when using double quotes in PHP.
$sql="SELECT * FROM spr
WHERE company='{$_SESSION[company]}'
AND branch='{$_SESSION[branch]}' AND owner IS NULL
ORDER BY id ";
And your ORDER BY clause needs to come at the end.

Related

Why it shows me error when preparing a sentence in PHP with MySQLi

I'm creating a sentence prepared in PHP, and I run into a rare syntax error, I do not know if it is breaching any of MySQL or why I show that error
The syntax is as follows, I want to sort by row and by ascending or descending type and limit the results
$query = "SELECT * FROM myTable ORDER BY ? ? LIMIT? ,?"
if($conn->prepare($query)){ .. } // error
The error is
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 '? LIMIT ? , ?' at line 1
If you execute said statement in MySQL, it correctly throws the results
Parameters to ORDER BY are not values, and cannot be parametrised. One is a column reference, the other is a keyword.
For example do like this and try.
$query = "SELECT * FROM myTable ORDER BY column_name LIMIT 0,10";

Mysql query strange error

I am getting an error when other same page is working good but another gives an error on same query code.
Here is my code what is wrong with this?
$ttt = mysql_query("SELECT * FROM like WHERE (user_id='$user_id' AND sound_id='$sound_id')",$link) or die(mysql_error());
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 'like WHERE (user_id='' AND sound_id='')' at line 1
like is an SQL reserved word and you should use "like" inside backticks ``
$ttt = mysql_query("SELECT * FROM `like` WHERE (user_id='$user_id' AND sound_id='$sound_id')",$link) or die(mysql_error());
like
Is a reserved word and cannot be used as a tablename the way you try to. Either try setting it into backticks or rename the table.
like is a reserved keyword use backtick for it
`like`
https://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
Usage of LIKE in mysql
select * from table where username like '%aaa';
select * from table where username like '%aaa%';
select * from table where username like 'aaa%';
etc
As a rule you shouldn't use reserved words, but if you must, and for the purpose of this question, put brackets around it.
$ttt = mysql_query
("SELECT *
FROM [like]
WHERE (user_id='$user_id' AND sound_id='$sound_id')",$link) or die(mysql_error());
Like is reserved word. Better to change your table name or surrounded with back tick like this like
Try this.
$ttt = mysql_query("SELECT * FROM like_table WHERE user_id=$user_id AND sound_id=$sound_id",$link) or die(mysql_error());

SQL simple query displaying error (CONTAINS function)

I am trying to display all users with #devon.gov.uk address.
I am using following code:
SELECT * FROM `j25_users` WHERE `email` CONTAINS `#devon.gov.uk`
But I am getting following 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 'CONTAINS `#devon.gov.uk` LIMIT 0, 30' at line 1
What could that indicate? SQL newbie here.
try this
SELECT * FROM `j25_users` WHERE CONTAINS (email,`#devon.gov.uk`)
You should use:
SELECT * FROM `j25_users` WHERE `email` like '%#devon.gov.uk%'
You tagged your post phpmyadmin, so while the others have correctly provided the SQL way of doing this, I'll just point out that phpMyAdmin also gives you a graphical interface. From the "Search" tab, just scroll down to the "email" row and change the dropdown to Like %...% and put #devon.gov.uk in the Value textbox. Then press Go.
You could try this instead:
SELECT * FROM `j25_users` WHERE `email` LIKE `%#devon.gov.uk%`;
The problem was the backtick `. the above code should have been:
SELECT * FROM `j25_users` WHERE `email` LIKE '%#devon.gov.uk%';

Why doesn't this MySQL select query work with WHERE clause?

Hello this is driving me crazy. I can't get this query to work with the WHERE clause. It works without it. I have tried everything. I have looked at dozens of websites and dozens of questions here. I can't see anything wrong with this query. But it gives me this error whenever I try to use a WHERE clause:
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 'WHERE `cond` = '1'' at line 4
I have tried it with spaces and without, with single quotes and without, I have tried mysqli. I just can't figure out what the problem is.
Here is the code I currently have:
$sql = <<<SQL
SELECT *
FROM `master_inv`
ORDER BY `sku`
WHERE `cond` = '1'
SQL;
WHERE clause goes before ORDER BY.
$sql = <<<SQL
SELECT *
FROM `master_inv`
WHERE `cond` = '1'
ORDER BY `sku`
SQL;
Your SQL statement is out of order and your missing a select statement.
The ORDER BY statement should be placed after the WHERE clause
SELECT *
FROM master_inv
WHERE cond = '1'
ORDER BY sku
It is just the order which is incorrect try:
SELECT *
FROM `master_inv`
WHERE `cond` = '1'
ORDER BY `sku`

Inner join in mysql, syntax error, regarding php query

$threadID=$_GET['threadID'];
$result=mysql_query("
SELECT * FROM threads AS Threads
INNER JOIN users AS Users ON Threads.user_id=Users.user_id
WHERE thread_id='$threadID' LIMIT 1
") or die(mysql_error());
I get this:
u have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near ''11' at
line 3
I wrote many inner joins before. Why is my syntax wrong near the end of the query
updated: 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 '12' LIMIT 1' at line 3
First of all, your query is WIDE open to sql injection attacks.
Second, to figure out why you get the syntax error, make the query building phase separate from the actual query call:
$sql = "SELECT ....";
echo $sql;
$result = mysql_query($sql) or die(...);
This way you can see the entire query. MySQL's error messages only report the portion of the query from where it thinks the error is onwards, but sometimes it decides wrong and elminates the actual relevant part where th error is... so... examine the ENTIRE query.
ah, may be I know the answer.
There is a thing in mysql, i believe, called "strict mode" or something, which being too picky about data types.
Try to make your query like this
$id = (int)$_GET['threadID'];
$sql = "SELECT * FROM threads t INNER JOIN users u ON t.user_id=u.user_id
WHERE thread_id=$id LIMIT 1";
$res = mysql_query($sql) or trigger_error(mysql_error().$sql);
Why not join your query in this fashion:
$threadID = mysql_real_escape_string( $_REQUEST['threadID'] ); // Get some cleanup in there...
$result=mysql_query("SELECT threads.*, users.*
FROM threads, users
WHERE threads.user_id = users.user_id
AND threads.thread_id = '$threadID'
LIMIT 1
") or die( mysql_error() );

Categories