I'd like to create a trigger from the following PHP code.
$sql = 'delimiter $$';
$pdo->exec($sql);
$sql = 'create trigger avoid_empty_employee_insert before insert on `employee`
for each row begin
if name = "" then set name = null; end if;
end$$';
$pdo->exec($sql);
$sql = 'delimiter ;';
$pdo->exec($sql);
When I run the the code in MySQL it works and the trigger is created.
PHP shows the following 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 'delimiter $$' at line 1
How can I fix it?
Definitely do not try to change the delimiter when you're executing the statement via the API. DELIMITER is a mysql client built-in command, it is not recognized by the MySQL server-side parser.
You don't need it anyway. The purpose of DELIMITER is to remove the ambiguity of semicolons that may appear within the body of a trigger or stored routine. Since the API is for executing one statement at a time, there's no ambiguity. The SQL parser just treats the whole string as one statement anyway.
Likewise, do not end the create trigger statement with $$. You don't need any statement terminator, but the SQL parser accepts ; as an optional statement terminator because so many people put it there even though they don't have to.
The next problem is that you when you use column names in a trigger, you have to prefix them with either NEW. or OLD. -- in an insert trigger, you can only use NEW. If you don't prefix the column, MySQL assumes you meant to set a system variable like tmpdir or slow_query_log.
If you are still getting the 1193 error, I suggest that you didn't change both references to the name column to NEW.name.
I tested the following using PHP 5.4.24 and MySQL 5.6.20, and it worked:
$sql = "create trigger avoid_empty_employee_insert before insert on `employee`
for each row begin
if NEW.name = '' then set NEW.name = null; end if;
end";
$pdo->exec($sql);
You don't need to delimit the column name of name, because it is not a MySQL reserved word. The set of reserved words is documented.
try that :
$sql = 'create trigger avoid_empty_employee_insert before insert on `employee`
for each row begin
if name = "" then set name = null; end if; END;';
$pdo->exec($sql);
Since you are creating the trigger using code, you might not need to set/reset the DELIMITER.
Just ignore those lines in your php code that changes them.
Your code becomes only:
$sql = 'create trigger avoid_empty_employee_insert before insert on `employee`
for each row begin
if NEW.`name` = "" then set NEW.`name` = null; end if;
end$$';
$pdo->exec($sql);
Mayby this helps:
$sql = "delimiter //\n";
$pdo->exec($sql);
$sql = "create trigger avoid_empty_employee_insert before insert on employee\n
for each row\n
begin\n
if new.name = '' then\n
set new.name = null;\n
end if;\n
end//\n";
$pdo->exec($sql);
$sql = 'delimiter ;';
$pdo->exec($sql);
Since you are creating the trigger using code, you might not need to set/reset the DELIMITER. Changing the DELIMITER is important for CLIs.
Just ignore those lines in your php code that changes them.
Your code becomes only:
$sql = 'create trigger `avoid_empty_employee_insert` before insert
on `employee`
for each row
begin
if `name` = "" then set `name` = null; end if;
end';
$pdo->exec($sql);
Related
I try to write a stored procedure like this but it does not work:
DROP PROCEDURE IF EXISTS `my_test`;
CREATE PROCEDURE `my_test`(
IN my_in_var VARCHAR(255),
OUT my_out_var VARCHAR(255)
)
BEGIN
IF(in_var == 'my_in_value') THEN
SET my_out_var = 'my_out_value1';
ELSE
SET my_out_var = 'my_out_value2';
END IF;
END
I try to execute from php with PDO
$conn = new PDO("mysql:host=".$mysql_host.";dbname=".$mysql_bd, $mysql_user, $mysql_password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->exec($sql);
no errors but I can't see it on MySql server with
SHOW CREATE PROCEDURE my_test
So I tried to copy paste it in the SQL window of phpmyadmin and I got this syntax 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 '== 'my_in_value') THEN
SET my_out_var = 'my_out_value1'' at line 6
What is the correct syntax? And why PDO not show the error ?
Any help is welcome - thanks
Note, it has been pointed out to me that the below DELIMITER is not necessary in PHPMyAdmin which I don't use. So, DELIMITER is a client thing, such as needed by the likes of MySQL Workbench.
Stored Proc:
DROP PROCEDURE IF EXISTS `my_test`;
DELIMITER $$
CREATE PROCEDURE `my_test`(
IN my_in_var VARCHAR(255),
OUT my_out_var VARCHAR(255)
)
BEGIN
IF (my_in_var = 'my_in_value') THEN
SET my_out_var = 'my_out_value1';
ELSE
SET my_out_var = 'my_out_value2';
END IF;
END$$
DELIMITER ;
Test:
set #outme='';
call my_test('lizard',#outme);
select #outme;
-- my_out_value2
set #outme='';
call my_test('my_in_value',#outme);
select #outme;
-- my_out_value1
So you need to figure out what your intention is with the above.
You had a syntax error with the double =. And you were perhaps having a typo in the in_var that did not exist.
PHPMyAdmin (that does not require DELIMITER so I am told):
DROP PROCEDURE IF EXISTS `my_test`;
CREATE PROCEDURE `my_test`(
IN my_in_var VARCHAR(255),
OUT my_out_var VARCHAR(255)
)
BEGIN
IF (my_in_var = 'my_in_value') THEN
SET my_out_var = 'my_out_value1';
ELSE
SET my_out_var = 'my_out_value2';
END IF;
END
I'm trying to change charset of my mysql database using a procedure.
I wrote it few years ago and did not go back to it, till today.
By this I have a weird mistake :
below is my procedure :
Create procedure changecharset()
Begin
Declare tname varchar(255);
Declare done int default 0;
Declare tc Cursor For
Select TABLE_NAME From information_schema.TABLES Where TABLE_SCHEMA = schema();
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
Open tc;
myloop: loop
Fetch tc into tname;
If done = 1 Then
Leave myloop;
End if ;
Set #query = Concat('Alter Table `', tname, '` convert to character set utf8 collate utf8_swedish_ci;');
PREPARE alterstmt FROM #query;
execute alterstmt ;
Deallocate prepare alterstmt;
End loop;
close tc;
End //
Delimiter ;
[/sql]
and this is the answer of mysql server
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 '' at line 3
Anykind of help will be much appreciated
It looks like MySQL is seeing the semicolon as the end of the statement.
Make sure you change the delimiter before running the CREATE PROCEDURE statement.
DELIMITER $$
CREATE PROCEDURE foo()
BEGIN
DECLARE ... ;
SET ... ;
END $$
DELIMITER ;
I have tried to create the following trigger but it is not working:
delimiter //
CREATE TRIGGER DeleteFeature
AFTER DELETE ON Features
FOR EACH ROW BEGIN
IF (OLD.FeatureID IN (SELECT FeatureID FROM GroupFeatures)) THEN
INSERT INTO DeletedFeatures VALUES (OLD.FeatureID, OLD.FeatureName, OLD.FeatureDescription);
END IF;
END;
//
It is giving the following 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 '//' at line 8 "
Any ideas what the problem could be?
Have you checked by removing the semi-colon and add $$ after the END
DELIMITER $$
CREATE TRIGGER DeleteFeature
AFTER DELETE ON Features
FOR EACH ROW
BEGIN
IF (OLD.FeatureID IN (SELECT FeatureID FROM GroupFeatures)) THEN
INSERT INTO DeletedFeatures VALUES (OLD.FeatureID, OLD.FeatureName OLD.FeatureDescription);
END IF;
END $$
DELIMITER ;
SQL Query -
CREATE TRIGGER `trigger_insert` AFTER INSERT ON `user`
FOR EACH ROW BEGIN
INSERT INTO `credentials` (`UserId`,`Password`,`UserType`,`Status`)
VALUES (NEW.UserId,NEW.Password,'2',NEW.Status);
END;
DELIMITER ;
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 '' at line 3
Need help... thanks in advance :)..
For your case you can use this statement -
CREATE TRIGGER `trigger_insert` AFTER INSERT ON `user`
FOR EACH ROW
INSERT INTO `credentials` (`UserId`,`Password`,`UserType`,`Status`) VALUES (NEW.UserId,NEW.Password,'2',NEW.Status);
What is the DELIMITER in MySQL and what it’s used for.
Delimiter problems I reckon:
DELIMITER $$
CREATE TRIGGER `trigger_insert` AFTER INSERT ON `user`
FOR EACH ROW BEGIN
INSERT INTO `credentials` (`UserId`,`Password`,`UserType`,`Status`) VALUES (NEW.UserId,NEW.Password,'2',NEW.Status);
END$$
DELIMITER ;
I got this request:
"START TRANSACTION; DELETE FROM `awaiting_auth` WHERE `code` = '06b8465eed00727a1eac49fae89b88f876ded2eb' LIMIT 1; INSERT IGNORE INTO `prsn` SET `login` = 'new_user', `passwd` = '40bd001563085fc35165329ea1ff5c5ecbdbbeef', `color` = '#cbc5f2'; COMMIT;"
And I receive 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 'DELETE FROM awaiting_auth WHERE code =
'06b8465eed00727a1eac49fae89b88f876de' at line 1".
However when executing sql via terminal everything goes well and no errors are thrown.
What is wrong with my request? Thanks in advance.
You must split the queries in separate ->query() calls. The SQL console does that automatically. E.g.
->query("START TRANSACTION");
->query("DELETE FROM...");
One operation per query. So you need to split your query into single queries.
Just run these queries one by one, not in single packet.
Try to execute each statement individually:
START TRANSACTION;
DELETE FROM `awaiting_auth` WHERE `code` = '06b8465eed00727a1eac49fae89b88f876ded2eb' LIMIT 1;
INSERT IGNORE INTO `prsn`(`login`, `passwd`, `color`) VALUES('new_user','40bd001563085fc35165329ea1ff5c5ecbdbbeef', '#cbc5f2');
COMMIT;