SQLSTATE[42000] Error - php

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).

Related

PHP Yii1 Syntax error or access violation: 1064

this is my code:
TableName::db()->updateAll(array('updated' => 'NOW()'), "WHERE userID
= ". (string)$id);
This is the errormessage i get:
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 MariaDB server
version for the right syntax to use near 'WHERE userID = 1043' at line
1. The SQL statement executed was: UPDATE TableName SET updated=:yp0 WHERE WHERE userID = 1043;. Bound with :yp0='NOW()'
The SQL Update Query will succesfully executed, but i want to fix this error.
Somebody have a hint for me how to fix this error?
Solution:
TableName::model()->updateAll(array('updated' => new CDbExpression('NOW()')), "userID= ". (string)$id);
The SQL Update Query will succesfully. Good luck to you

'PDOException' Syntax error or access violation: 1064 You have an error in your SQL syntax; check

I keep getting the following error when trying to submit details of an order into my database:
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 ' in /home/ubuntu/workspace/handlers/checkout-handler.php on line 111 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 'order (order_details, order_address, cust_id, cust_name, delivery_type, paid) ' at line 1.
I can't figure out whats wrong with it, all of the variables are being posted correctly to the page.
$query1 = "INSERT INTO order (order_details, order_address, cust_id, cust_name, delivery_type, paid) VALUES(:details,:address,:d,:name,:delivery,:paid);";
$sql=$conn->prepare($query1);
$sql->bindParam(':details', $details);
$sql->bindParam(':address', $address);
$sql->bindParam(':name', $name);
$sql->bindParam(':delivery', $delivery_type);
$sql->bindParam(':paid', $paid);
$sql->bindParam(':d', $d);
$sql->execute();
order is a reserved keyword. You should add backticks ` around it to use it:
$query1 = "INSERT INTO `order` (order_details, order_address, cust_id, cust_name, delivery_type, paid)
VALUES(:details,:address,:d,:name,:delivery,:paid);";
$sql = $conn->prepare($query1);
See also : Keywords and Reserved Words

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.

How to truncate a table with a prepared statement in Doctrine2

I'm doing:
$truncateSQL = 'TRUNCATE TABLE :tableName';
$stmtTruncate = $em->getConnection()->prepare($truncateSQL);
$stmtTruncate->bindValue('tableName',$this->tableName);
$stmtTruncate->execute();
But getting the error:
[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 ''image_sizes_t'' at line 1
Are the quotes round the table name the problem here? $this->tableName is just a string
You can't use table or column names in as placeholders in prepared statements as 'table_name' is invalid MySql syntax.
if you need to make your column / table names safe you can wrap them in backticks.
"`".$table_name."`"

Trying to insert IP address but get an error

I'm trying to insert IP addresses into LastIP(An unsigned integer)
INSERT INTO user_entry (UPC, StateID, StoreID,CityID,Price,Count,LastIP) VALUES (885909301378,1,1,1,170,0,INET_ATON(127.0.0.1))
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 '.0.1))' at line 1
You need to add quotes:
INSERT INTO user_entry
(UPC, StateID, StoreID,CityID,Price,Count,LastIP) VALUES
(885909301378,1,1,1,170,0,INET_ATON("127.0.0.1"))
Source: Manual

Categories