where is the syntax error in this two query used together - php

Code runs in phpmyadmin but not running in my php code and says the code has syntax problem.
What is the problem ?
INSERT INTO `users`(`username`, `email`, `password`) VALUES ( 'test' , 'test#email.com' , '13456');
UPDATE `users` SET `email`='test2#email.com'
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 'UPDATE `users` SET `email`='test2#email.com'' at line 1 (SQL: INSERT INTO `users`(`username`, `email`, `password`) VALUES ( 'test' , 'test#email.com' , '13456'); UPDATE `users` SET `email`='test2#email.com')

You can't run two queries from PHP in one call, unless you use mysqli_multi_query(). Split them into two separate calls.

Related

How do you format BEGIN ... END statements using PDO's with SQL?

This works fine:
...
$sql = "INSERT INTO person (`name`, `age`)
VALUES ('John', 34);";
$stmt = $conn->prepare($sql);
...
But this gives an error:
...
$sql = "BEGIN
INSERT INTO person (`name`, `age`)
VALUES ('John', 34);
END;";
$stmt = $conn->prepare($sql);
...
The error is:
SyntaxError: unterminated string literal
And more specifically:
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 'INSERT INTO person (`name`, `age`)
I know that the BEGIN ... END is unnecessary in this contrived example, but why does it not work?
Thanks very much for any help.
In PDO, like in any other API, you run separate queries using separate API calls:
Given the END keyword makes no sense and assuming you meant COMMIT,
$conn->query("BEGIN");
$conn->query("INSERT INTO person (`name`, `age`) VALUES ('John', 34)");
$conn->query("COMMIT");
However, you have to keep in mind that
there is no point in running a transaction for a single query
there is no point in using a prepared statement if you bind no data to it

Another generic MySQL error w/ PHP PDO

I'm getting MySQL error 42000:1064 that suggests a general syntax error with the following SQL:
UPDATE `events` SET ?=?, ?=?, ?=now() WHERE `event_id`=?;
PHP code to convert to a readable statement & also execute:
<?php
$ar = array_fill(0,count($args),'/\?/');
echo preg_replace($ar,$args,$sql,1);
$this->execute($sql, $args);
?>
This evaluates to:
UPDATE `events` SET event_name=test, form_id=webform, last_updated=now() WHERE `event_id`=124;
Which when pasted into the MySQL workbench completes successfully.
[mysqlErrorMsg] => 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 ''event_name'='test', 'form_id'='webform', 'last_updated'=now() WHERE `event_id`=' at line 1
It should be noted that my user has full access to the table in question.
You can't use placeholders on column names. Only on values.
Your query does NOT evaluate to (as it should)
UPDATE `events` SET event_name=test, form_id=webform, last_updated=now()
WHERE `event_id`=124;
but is being evaluated as this instead:
UPDATE `events` SET 'event_name'='test', 'form_id'='webform', 'last_updated'=now()
WHERE `event_id`=124;
See the quotes? These are strings, not column names.
So hard code the column names and only use placeholders for values
UPDATE `events` SET event_name=?, form_id=?, last_updated=now() WHERE `event_id`=?;

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

UUID_SHORT regenerate if duplicate

I have this query:
$query = "INSERT INTO users (users_uuid, type_id) VALUES (UUID_SHORT(), :type_id)";
My question is if in case the UUID_SHORT() generated already exists, is there any way to tell MySQL to generate another UUID_SHORT() within that query? What I have in my mind now is to trap the return error response then execute again the query, which I find inefficient.
Based #eicto comment, I read ON DUPLICATE KEY UPDATE then tried to reconstruct my query, I achieve a new query:
$query = "INSERT INTO users (users_uuid, type_id) VALUES (UUID_SHORT(), :type_id) ON DUPLICATE KEY UPDATE (users_uuid) = VALUES(UUID_SHORT())";
However I received an error in my log that states:
"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 '(users_uuid) = VALUES(UUID_SHORT())' at line 1"
What does this mean?

CodeIgniter not correctly performing a sql query

In Ci I'm getting the following error:
A Database Error Occurred
Error Number: 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 '', '1f37540fc54292b018a9e993da38cb5647dc5587', NOW(), '0')' at line 1
INSERT INTO users (userpid, Email, Password, dateTimeRegistered, isLeader) VALUES ('147344f0e33367f9e6', erf', '1f37540fc54292b018a9e993da38cb5647dc5587', NOW(), '0')
Filename: C:\Workspace\htdocs\Misc-2\DruvlaCi-1\system\database\DB_driver.php
Line Number: 330
But everything seems to be right in the SQL query. Below is my query, any thoughts?
$query = $this->db->query("INSERT IGNORE INTO users (userpid, Email, Password, dateTimeRegistered, isLeader) VALUES ('{$idgen}', {$postedEmail}', '{$hashedPass}', NOW(), '0')");
You're forgetting the opening single quote for the email column
Try this instead
$query = $this->db->query("INSERT IGNORE INTO users (userpid, Email, Password, dateTimeRegistered, isLeader) VALUES ('{$idgen}', '{$postedEmail}', '{$hashedPass}', NOW(), '0')");
Uhm, and please, use this syntax for inserting instead:
http://codeigniter.com/user_guide/database/active_record.html#insert
works so much safer.

Categories