in php5, how to use mysql_execute in script that has delimiter
Have a script that I can copy and run in mysql shell just fine, but explodes when attempting the same script in php5 mysql_query
. Part of the script:
-- sync shadow with users
drop trigger users_post_insert;
delimiter $$
create trigger users_post_insert after insert on users
for each row
begin
insert into shadow( usr_id ) values( new.usr_id );
end$$
delimiter ;
Raises 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
'delimiter $$\ncreate trigger users_post_insert after insert
on users\n\tfor each ro' at line 8
Again, have done similar executing script files with Postgr开发者_StackOverflowesQL and Oracle scripts, so this took me off guard.
Do MySQL scripts need to be passed through a regex
before being run, or what?
These are scripts that have been debugged and then applied via php
to new schemas.
Might you need to define delimiter first before executing a command, and make sure you dropped an already created trigger (or just use if exists clause) try this
-- sync shadow with users
delimiter $$
drop trigger if exists users_post_insert;$$
create trigger users_post_insert after insert on users
for each row
begin
insert into shadow( usr_id ) values( new.usr_id );
end;$$
delimiter ;
Testing shows mysql_execute
cannot accept the DELIMITER command.
To make it work break up your file into chunks, leaving out the DELIMITER lines altogether. For example,
$procs = file_get_contents( '../sql/security.sql' );
$procArr = preg_split( "/delimiter .*\n/", $procs );
Also, remove the set delimiter "$$" from the remaining chunks.
Feed each non-empty chunk into mysql_execute
- it's ugly, but it works.
精彩评论