While the following query works with phpmyadmin, when I use mysqli->query(), a syntax error occurs
START TRANSACTION;
SELECT Value INTO #Increment FROM SystemConfiguration WHERE `Key` = 'POIncrement' FOR UPDATE;
UPDATE SystemConfiguration SET Value = Value + #Increment WHERE `Key` = 'POID';
COMMIT;
The syntax error message:
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 'SELECT Value INTO #Increment FROM SystemConfiguration WHERE `Key` = 'POIncrement' at line 2
Is it that mysqli prepares the query and adds something in?
$sql = <<<SQL
START TRANSACTION;
SELECT Value
INTO #Increment
FROM SystemConfiguration
WHERE `Key` = 'POIncrement' FOR UPDATE;
UPDATE SystemConfiguration
SET Value = Value + #Increment
WHERE `Key` = 'POID';
COMMIT;
SQL;
$res = mysqli_multi_query($connection, $sql);
Related
Trying to update the record (timestamp) if it exists or insert a new record if it doesn't exist.
Table is:
id = int 12 primary key, auto increment
userid = int 12
viewerid = int 12
viewDateTime = TIMESTAMP
This sql works in phpmyadmin but not in php
SELECT #id := id FROM `profileViews` WHERE `userid` = 31 AND `viewerid` = 30 LIMIT 1;
REPLACE INTO `profileViews`(id, `userid`, `viewerid`, `viewDateTime`)
VALUES (#id, 31, 30, now());
Here is the php version:
$INSERTViewSQL = "SELECT #id := id FROM `profileViews` WHERE `userid` = ? AND `viewerid` = ? LIMIT 1;
REPLACE INTO `profileViews`(id, `userid`, `viewerid`, `viewDateTime`)
VALUES (#id, ?, ?, now());";
try{
$DBConnection->prepare($INSERTViewSQL)->execute([$profileid, $_SESSION["id"], $profileid, $_SESSION["id"]]);
} catch(PDOException $e) {
file_put_contents($ErrorLogFileForPDO, 'update view : ' .$e->getMessage()."\n", FILE_APPEND);
}
Here is the error message:
update view : 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 'REPLACE INTO profileViews(id, userid, viewerid, viewDateTime)
VALUES (#i' at line 2
Thanks‼️
MySQL document says:
SQL syntax for prepared statements does not support multi-statements (that is, multiple statements within a single string separated by ; characters).
So you need to fetch id value first, execute replace statement after that.
$stmt = $DBConnection
->prepare("SELECT id FROM ...");
$stmt->execute([$profileid, $_SESSION["id"]]);
$id = $stmt->fetchColumn();
$DBConnection
->prepare("REPLACE INTO ...");
->execute([$id, $profileid, $_SESSION["id"]]);
I'm trying to set the column of a table to a string in every row if the column exists. However, I am receiving SQL syntax errors and can't pinpoint what exactly the issue is:
while($db = mysqli_fetch_array(mysqli_query($conn, "SHOW DATABASES"))){
$id = $db['id'];
$sql = /** #lang text */
"SELECT IF( EXISTS(
SELECT DISTINCT `info`
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('borrower')
AND TABLE_SCHEMA = '$db'))
BEGIN
UPDATE `info`
SET `borrower` = '****'
WHERE `id` = '$id'
END";
$query = mysqli_query($conn, $sql);
if (!$query){
// Deal with error
}
}
The syntax error I receive back:
information_schemadb1You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')BEGINUPDATE infoSET borrower = '****'' at line 5
I know it must be something very small but I have been stuck for a while and can't seem to find my error.
Try this syntax for the query-
IF EXISTS (SELECT DISTINCT `info`
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('borrower')
AND TABLE_SCHEMA = '$db')) THEN
UPDATE `info`
SET `borrower` = '****'
WHERE `id` = '$id'
END IF;
I added Then in the end of the if condition and End if instead of only if, and I removed the select to. What are you trying to select? it is only update clause.
update
Example from here
you can't use it like a normal query. Control structures like IF or
WHILE are only allowed in stored procedures or functions.
MYSQL panel
delimiter $$
create procedure YourIfConditionSP()
begin
IF EXISTS (SELECT DISTINCT `info`
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('borrower')
AND TABLE_SCHEMA = '$db') THEN
UPDATE `info`
SET `borrower` = '****'
WHERE `id` = '$id';
END IF;
end $$
delimiter ;
And you have to change your php code for calling the stored procedure.
PHP
//run the stored procedure
$result = mysqli_query($connection,
"CALL YourIfConditionSP") or die("Query fail: " . mysqli_error());
How to call a MySQL stored procedure from within PHP code?
this is my upadating script--
$cno = $data[6];
$result = mysql_query("select *
from courier_details
where consignment_no = '".$cno."'");
if($result >0)
{
$update = "UPDATE `courier_details`
SET(`shipper_name`, `shipper_phone`, `shipper_address`, `receiver_name`,
`receiver_phone`, `receiver_address`, `consignment_no`,
`type_of_shippment`, `weight`, `volumetric_weight`, `packages`,
`product`, `qnty`, `booking_mode`, `total_freight`, `mode`,
`dept_time`, `origin`, `destination`, `pickup_date`, `pickup_time`,
`status`, `excepted_dly_date`, `comments`, `delivered_date`,
`delivered_time`, `deboy`, `career`,`originbr`, `destinationbr`,
`email`)
VALUES('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]',
'$data[6]','$data[7]','$data[8]','$data[9]','$data[10]','$data[11]',
'$data[12]','$data[13]','$data[14]','$data[15]','$data[16]',
'$data[17]','$data[18]','$data[19]','$data[20]','$data[21]',
'$data[22]','$data[23]','$data[24]','$data[25]','$data[26]',
'$data[27]','$data[28]','$data[29]','$data[30]')
WHERE `consignment_no` = '".$cno."'";
mysql_query($update) or die(mysql_error());
}
there is show an error while execute code--
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 '(shipper_name, shipper_phone, shipper_address, receiver_name, `receiver_' at line 1
Your update query seems wrong.
You cannot use UPDATE query like INSERT query. Syntax should be:
Update TableName
Set col1=val1,
col2=val2,
col3=val3,
.......
On the other hand, INSERT can be used like:
INSERT INTO TableName
(col1,col2,col3) VALUES (val1,val2,val3)
You were mixing update and insert query you need to learn about the differences between them
UPDATE
UPDATE table_name SET field1=new-value1, field2=new-value2
[WHERE Clause]
INSERT
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)]
VALUES (value1, value2, value3,...valueN);
So your query will be like
"UPDATE `courier_details` SET `shipper_name` = '$data[0]',
`shipper_phone` = '$data[1]'.....
WHERE `consignment_no` = '$cno'"
use mysql_num_rows() to count number of row
this is wrong
if($result >0)
And don't use update query as insert query both are different
It would be:-
$result = mysql_query("select * from courier_details where consignment_no = '" . $cno . "'");
if (mysql_num_rows() > 0) {
$update="UPDATE table_name SET field1=value1, field2=value2";
mysql_query($update) or die(mysql_error());
}
$column = "`0907001`='0',`0907002`='0',`0907003`='0',`0907004`='0',`0907005`='0'";
$date="01/01/2013";
$sql_cmd = "UPDATE `$database`.`$table` SET ($column) WHERE `$table`.`Date` = '$date'";
if(!mysql_query($sql_cmd)) {
die('inside AddUserToDataBase Error: ' . mysql_error());
}
Here I got an 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 '(`0907001`='0',`0907002`='0',`0907003`='0',`0907004`='0',`0907005`='0') WHERE `C' at line 1
Please Help....
How can I solve this problem........
Just drop the ( ) around $columns in the query:
$sql_cmd = "UPDATE `$database`.`$table` SET $column WHERE `$table`.`Date` = '$date'";
Remove the parenthesis around the columns. Instead of:
UPDATE TABLE table SET (column = value)
It should be
UPDATE TABLE table SET column = value
I can't get this to compile properly.
$username = mysql_real_escape_string($_GET['username']);
$about = mysql_reaL_escape_string($_GET['about']);
$icebreaker = mysql_reaL_escape_string($_GET['icebreaker']);
$query = "UPDATE '$mysql_database'.main SET about = '$about', icebreaker = '$icebreaker' WHERE username = '$username';";
I get the 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 ''a4396957_users'.main
SET about = 'testing', icebreaker = 'ice' WHERE us' at line 1
Is there something missing in this statement?
don't quote the database name with apostrophes or quotes
$query = "UPDATE `$mysql_database`.`main` SET `about` = '$about', `icebreaker` = '$icebreaker' WHERE `username` = '$username';";
Use ` instead of '
$query = "UPDATE $mysql_database.main SET about = '$about', icebreaker = '$icebreaker' WHERE username = '$username';";
Certain objects within MySQL, including database, table, index, column, alias, view, stored procedure, partition, and other object names are known as identifiers..
The identifier quote character is the backtick (“`”)..
If the ANSI_QUOTES SQL mode is enabled, it is also permissible to quote identifiers within double quotation marks:
mysql> CREATE TABLE "test" (col INT);
ERROR 1064: You have an error in your SQL syntax...
mysql> SET sql_mode='ANSI_QUOTES';
mysql> CREATE TABLE "test" (col INT);
Query OK, 0 rows affected (0.00 sec)