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

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`

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

PHP : Error in Query (MySQL) near WHERE clause

I am trying to Insert data from a form with the use of a query. The query ( below ) has a WHERE clause to pick a position from visitorsystem.position.
$query = "INSERT INTO visitorsystem.employee(idNumber,name,surname,position,email)
VALUES ('$idNumber','$name','$surname',SELECT positionid FROM visitorsystem.position WHERE position LIKE '%$position%','$email')";
When executed the following error is given. I have tried adding quotes and single quotes around the SELECT...WHERE clause with no luck. Any ideas if the problem is with the query itself or the SELECT...WHERE clause ?
Error in 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 '','fdsf',SELECT positionid FROM visitorsystem.position WHERE
position LIKE '%inf' at line 2
Change your query to :
$query = "INSERT INTO visitorsystem.employee(idNumber,name,surname,position,email)
SELECT '$idNumber','$name','$surname',positionid,'$email' FROM visitorsystem.position WHERE position LIKE '%$position%'";
First of all, learn about prepared Statements to prevent SQL injection.
Second you should add all values to the select Statement:
query = "INSERT INTO visitorsystem.employee(idNumber,name,surname,position,email)
SELECT $idNumber,'$name','$surname',positionid,'$email' FROM visitorsystem.position WHERE position LIKE '%$position%'";
Also you do not need singlequotes around $idNumber, because it is numeric

MySQL query errors with "You have an error in your SQL syntax"

I'm performing the following query and it seems I have an issue right where I'm doing the convert(aes_decrypt) part as the query was working fine before I added it in. I know it's something small but can't quite seem to get the statement correct.
Missing quotes or wrong quotes? I just can't see it..
$db->query("SELECT
`users`.`name` AS `users`.`name` ,
convert(aes_decrypt(`users`.`email`,".$salt.") using utf8 AS `users`.`email` ,
`users`.`address` AS `users`.`address`
FROM `users`");
The exact error I'm receiving 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 '.name ,convert(aes_decrypt(users.email,MySecretSa' at line
1
Alias names are user defined. Qualifiers on them is meaning less and not supported.
select users.column_name as users.column_name
from users -- this syntax is not correct
If you want just the same name as column name, omit alias name.
select column_name from table_name
And if you want explicit column/expression alias names don't use table qualifiers.
select column_name as alias_name
, col1 * col2 as expression_alias
from table_name
Yes ,it's strange: users.name AS users.name
The error in the second users.name is wrong.
can make it like this
$db->query("SELECT
`users`.`name` AS `user_name` ,
convert(aes_decrypt(`users`.`email`,".$salt.") using utf8 AS `users`.`email` ,
`users`.`address` AS `users`.`address`
FROM `users`");
Or you can eliminate like this
$db->query("SELECT
`users`.`name`,
convert(aes_decrypt(`users`.`email`,".$salt.") using utf8 AS `users`.`email` ,
`users`.`address` AS `users`.`address`
FROM `users`");
Try this sql query:-
$db->query("SELECT
`users`.`name` AS name ,
CONVERT( aes_decrypt(`users`.`email`, '$salt') USING utf8) AS email ,
`users`.`address` AS address
FROM `users`");

PHP ERROR in SQL syntax

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.

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