Symfony DBAL insert gives: "Syntax error or access violation: 1064" - php

I'm trying to insert some data into database, but something is off here. Cannot catch what it is and I'm hoping someone might be able to tell me what's going on with this one.
My table looks somewhat like this:
CREATE TABLE IF NOT EXISTS `db_name`.`order` (
`orderID` INT NOT NULL AUTO_INCREMENT,
`order_ordernumber` VARCHAR(200) NOT NULL,
`order_orderweight` INT NOT NULL,
... And other columns as well, but all NULL
ENGINE = InnoDB
I'm using Symfony2-framework and it's DBAL-insert here:
$conn->insert('order', array(
'order_ordernumber' => $this->orderid,
'order_orderweight' => $this->totalweight
));
"$this->orderid" is string variable, and "$this->totalweight" is int.
And it gives this error-message:
An exception occurred while executing 'INSERT INTO order (order_ordernumber, order_orderweight) VALUES (?, ?)' with params ["000001", 900]:
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 'order (order_ordernumber, order_orderweight) VALUES ('000001', '900')' at line 1
500 Internal Server Error - DBALException
1 linked Exception: PDOException ยป
I can do the same query on pure sql and it works, this one doesn't. What on earth is going on with this one?

order is a reserved keyword in MySQL : http://dev.mysql.com/doc/refman/5.6/en/reserved-words.html
You have to use backquotes arround the word to prevent this error. But a better recommendation would be to prefix your table.

Related

Access violation 1064 when using NULL in sql statement

I have a error in my SQL statement. I am using NULL in my command and I guess thats the problem, but I am not sure. So what am I doing wrong here ?
Code:
function run()
{
$sql = "UPDATE %%EVENT%% SET lock = NULL WHERE 'lock' IS NOT NULL";
Database::get()->update($sql);
}
Error:
USER 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 'lock = NULL WHERE 'lock' IS NOT NULL' at line 1
Try removing the apostrophes around the second 'lock':
UPDATE %%EVENT%% SET lock = NULL WHERE lock IS NOT NULL
Without knowing the SQL dialect you're using it's hard to further diagnose the issue. It's possible that lock is a reserved keyword. What are you trying to achieve with %%EVENT%%? I assume you're trying to use wildcard.

CakePHP2.x got syntax error when use Prepared Statement by query() method

I'm using CakePHP 2.9.9.
I want to use prepared statement in query method, but got syntax error.
code is below
$query = 'CREATE TABLE IF NOT EXISTS ? (id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY)';
$this->User->query($query, array('dynamic_table_name'));
error message is this.
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 ''dynamic_table_name' (id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY)' at line 1
Why escape by single quote like ''dynamic_table_name' ?
How to fix it?

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.

save and find uuid by mysql and yii

key column in table is char(36) utf8_general_ci
//save no problem
$key_Ad= Yii::app()->db->createCommand('select UUID()')->queryScalar();
$modelAd->key=$key_Ad;
$modelAd->save()
//but problem in find
$post=Ad::model()->find( "key = :key",array(':key'=>$key_Ad) );
---------------------->Error
CDbException
CDbCommand failed to execute the SQL statement: 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 'key = '3f837af1-8a16-11e4-b111-00241d5e096e' LIMIT 1' at line 1. The SQL statement executed was: SELECT * FROM ad t WHERE key = :key LIMIT 1 (C:\xampp\htdocs\framework\db\CDbCommand.php:543)
0 C:\xampp\htdocs\framework\db\CDbCommand.php(415): CDbCommand->queryInternal('fetch', Array, Array)#1 C:\xampp\htdocs\framework\db\ar\CActiveRecord.php(1351): CDbCommand->queryRow()#2 C:\xampp\htdocs\framework\db\ar\CActiveRecord.php(1456): CActiveRecord->query(Object(CDbCriteria))#3 C:\xampp\htdocs\agahi\protected\controllers\ImageController.php(34): CActiveRecord->find('key = :key', Array)#4 C:\xampp\htdocs\framework\web\actions\CInlineAction.php(49): ImageController->actionUploadImage()#5 C:\xampp\htdocs\framework\web\CController.php(308): CInlineAction->runWithParams(Array)#6 C:\xampp\htdocs\framework\web\CController.php(286): CController->runAction(Object(CInlineAction))#7 C:\xampp\htdocs\framework\web\CController.php(265): CController->runActionWithFilters(Object(CInlineAction), Array)#8 C:\xampp\htdocs\framework\web\CWebApplication.php(282): CController->run('UploadImage')#9 C:\xampp\htdocs\framework\web\CWebApplication.php(141): CWebApplication->runController('image/UploadIma...')#10 C:\xampp\htdocs\framework\base\CApplication.php(180): CWebApplication->processRequest()#11 C:\xampp\htdocs\agahi\index.php(13): CApplication->run()#12 {main}
The problem is that you have used the reserved mySql keyword 'key' as your column name. That's what generates the syntax error. It is best that you rename your column to something different than 'key', e.g. 'key1' or 'key_ad'.
In mySql you can still execute the query with the column named 'key' by escaping it in the select statement using '`', I'm not sure if you can do this in Yii, you should try it.
But the best solution is to just rename the column and not use reserved words as column names in the future.

Invalid PDO MySQL Statement - Error in Syntax

So, I have the following PDO statement, which is one of several similarly constructed statements on the page. The rest are working fine, so I am confused as to why this one is stating invalid syntax.
$test_id = '1';
$option = 'a';
$ab_email = 'test#testing.com';
$stmt = $pdo_db->prepare("INSERT INTO `ab_tests_visitors` (test_id,option,visits,email) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE email=?");
$stmt->execute(array($test_id,$option,1,$ab_email,$ab_email));
The schema for the database has 5 columns, 2 of which are indexes.
visitor_id is int(10) and auto_increment, and also primary
test_id is int(10)
option is varchar(100)
visits is int(10) and default of 1
email is varchar(255) and unique
The error being given is:
Fatal error: Uncaught exception 'PDOException' with message '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 'option,visits,email) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE email=?' at line 1'
option is a MySQL keyword. If you want to use it as an identifier, make sure to always surround it with backticks (as you should do with identifiers anyway):
$stmt = $pdo_db->prepare("INSERT INTO `ab_tests_visitors` (`test_id`,`option`,`visits`,`email`) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE `email`=?");
For this problem, in name of your column, you must use backticks ` around the column name (alt + 96)

Categories