SQLSTATE[42000]: Syntax error or access violation: - php

I've been trying to fix this SQL state error from the past 30 mins.
What am I doing wrong here?
Any suggestions are welcome.
$statement=$conn->prepare("SELECT to.ipaddress as ipaddress, to.email as email, to.orderdate as orderdate, to.is_completed as is_completed, p.price as price from temporder to, products p WHERE to.productid = p.productid AND to.ipaddress=? AND to.orderdate=? ");
$statement->execute(array(
$_SERVER['REMOTE_ADDR'],
date('Y-m-d')
));
$statement->setFetchMode(PDO::FETCH_ASSOC);
Complete error
SQLSTATE[42000]: Syntax error or access violation: 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 'to, products p WHERE to.productid = p.productid AND to.ipaddress='::1' AND to.or' at line 1

TO (which you are using as an ALIAS) is a reserved word in MySQL , you need to surround it in backticks.
From a part of your query...
e as price from temporder to, products p WHERE to
^^^^ //<----- That's a reserved word. !
Wrap them under backtick like below..
e as price from temporder `to`, products p WHERE to

to is a reserved key word
so u need to wrap it within backtics as
`to`
http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html

Related

CakePHP 3 Error: SQLSTATE[42000]: Syntax error or access violation: 1064

I'm getting this error:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'AS `Colleges__*` FROM college_admins CollegeAdmins LEFT JOIN colleges Colleges O' at line 1
Here is the SQL query which is giving this error:
SELECT Colleges.* AS `Colleges__*` FROM college_admins CollegeAdmins LEFT JOIN colleges Colleges ON Colleges.id = (CollegeAdmins.college_id) WHERE CollegeAdmins.user_id = :c0 LIMIT 20 OFFSET 0
I enabled quoteIdentifiers config\app, but it leads to this new error:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'AS `Colleges__*` FROM `college_admins` `CollegeAdmins` LEFT JOIN `colleges` `Col' at line 1
where the query becomes:
SELECT `Colleges`.* AS `Colleges__*` FROM `college_admins` `CollegeAdmins` LEFT JOIN `colleges` `Colleges` ON `Colleges`.`id` = (`CollegeAdmins`.`college_id`) WHERE `CollegeAdmins`.`user_id` = :c0 LIMIT 20 OFFSET 0
I think it's taking the 'Col from Colleges as the keyword 'COL', but I'm not sure. How to fix this?
This is the CakePHP code which is generating the MySQL query:
return $college_admins->find()
->select(['Colleges.*'])
->leftJoinWith('Colleges')
->where(['CollegeAdmins.user_id' => $userId]);
You cannot use Colleges.* in a CakePHP ORM query (CakePHP 3.x). As you've discovered this creates incorrect SQL aliases like Colleges__*. Instead to select all columns of a table you need to pass a table object.
So you'd probably be wanting to do something like:-
->select($college_admins->Colleges)
Assuming Colleges is associated with your CollegeAdmins table.
You cannot alias colleges.*, since this refers to all columns within colleges table and aliases refer to a single column (or table or subquery). You need to list all fields within the colleges table and provide an alias for each of them, such as
select colleges.ig as colleges_id, colleges.field1 as colleges_field1, ...
There is not syntax in sql to provide alias such way. What you may try to do is to access the metadata returned by mysql in php to retrieve the table name for each field.

SQL query: Query browser shows error 1064

I cant find what happened to the MYSQL query. It was working fine and today it is throwing 1064 error can anyone help me.
In query browser i am getting 1064 error
and in mysqli_error i am getting "Unknown column 's.firstname' in 'field list'".
query ::
SELECT s.recid userId, s.firstname firstName, s.lastname lastName, s.email email, s.status status
FROM usertable s
WHERE s.email = 'xyz#gmail.com'
AND s.mobile = 'XXXXXXXX'
AND (s.enddate IS NULL OR s.enddate = '0000-00-00' OR s.enddate > '2015-04-08 11:13:34')
Error :
Unknown column 's.firstname' in 'field list'
Check the firstname spelling, white spaces on start and end of the field, if its okay, then remove firstname from query and then try it once.
This will help us to debug.
Check your table has the column firstname
Use below command to check it within query browser
DESC usertable;
Try to wrap everything with backticks (or at least column name called status) and see what happens...
SELECT `s`.`recid` `userId`, `s`.`firstname` `firstName`, `s`.`lastname` `lastName`, `s`.`email` `email`, `s`.`status` `status`
FROM `usertable` `s`
WHERE `s`.`email` = 'xyz#gmail.com'
AND `s`.`mobile` = 'XXXXXXXX'
AND (`s`.`enddate` IS NULL OR `s`.`enddate` = '0000-00-00' OR `s`.`enddate` > '2015-04-08 11:13:34')
Note that you are using status as column name, but even if it isn't declared as keyword it is used in queries like SHOW STATUS...So I will avoid that and change its name to something else. Also using status like this in some versions of phpMyAdmin produced some unexpected results. But when you wrap it with backticks you can use it as column name.

You have an error in your SQL syntax - near 'left)

I'm a newbie in the php programming.
I would like to apply an insert query but 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 'left) VALUES ('****df','****2135gh','***#yahoo.com' at line 2"}
$sql_insert_new_user = "insert into users (username,password,email,status,finance,province,city,address,tell,
mobile,admin_seen,type,left) VALUES ('$username','$password','$email',1,0,$town,$city,
'$address','$telephone','$mobile',0,'employe',0)";
mysql_query($sql_insert_new_user);
$error = mysql_error();
left is a reserved word and in the query you need escape with backtics
`left`
https://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
left is a reserved word and in the query you need to use like this for all reserved word
'left'
This gives all the details about reserved word https://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
$sql_insert_new_user = "insert into users (username,password,email,status,finance,province,city,address,tell,
mobile,admin_seen,type,`left`) VALUES ('$username','$password','$email',1,0,$town,$city,
'$address','$telephone','$mobile',0,'employe',0)";
left is a mysql reserved word. So you have to rename the column left.
See here : reserved Words for furhter information

SQLSTATE[42000] Error

Cant figure out where this query is going wrong...
getting this error:
{"databaseException":"SQLSTATE[42000]: Syntax error or access violation: 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 'desc) \n VALUES (1, array)' at line 1"
from this query
$statement = $db->prepare(
"INSERT INTO `descriptions` (vrm, desc) VALUES (:vrm, :description)"
);
if ($statement->execute(array(
':vrm' => '1',
':description' => $_POST['desc'])));
Thanks!
You should add backticks to the desc column name. It's a reserved word (ORDER BY vrm DESC).

PDOException error in MySQL database

I am getting the following error message when executing my query
[2/2] DBALException: An exception occurred while executing 'SELECT DISTINCT s0_.id AS id0, s0_. AS 1 FROM shop s0_ WHERE s0_.isLocked = ? ORDER BY s0_.owner_id DESC LIMIT 20 OFFSET 0' with params [0]:
SQLSTATE[42000]: Syntax error or access violation: 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 'AS 1 FROM shop s0_ WHERE s0_.isLocked = 0 ORDER BY s0_.owner_' at line 1 +
[1/2] PDOException: SQLSTATE[42000]: Syntax error or access violation: 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 'AS 1 FROM shop s0_ WHERE s0_.isLocked = 0 ORDER BY s0_.owner_' at line 1
Is this supposed to be a query error or is this something else ? Because it seems that my query is legit.
SELECT DISTINCT s0_.id AS id0, s0_. AS 1
It is supposed to be a query error. You are selecting the unique id's from table s0_ and a field with no name of that same table, but a field can't have no name.
You should add a fieldname, like this:
SELECT DISTINCT s0_.id AS id0, s0_.FIELDNAME AS `1`
SELECT DISTINCT s0_.id AS id0, s0_. AS `1`
wrap 1 in ``
And the missing field name from comments above...

Categories