在PHP中使用PDO创建存储过程 [英] Create Stored Procedures with PDO in PHP
问题描述
我正在从PHP中读取一个TEXT文件,并试图从中执行命令,例如创建一个DB及其具有的所有表和过程.我的代码创建表,但不创建文件中给定的存储过程.
I am reading a TEXT file from PHP and trying to execute commands from it, like creating a DB and all the tables and procedures it has. My code creates the tables but does not create Stored Procedures given in the file.
DELIMITER $$
DROP PROCEDURE IF EXISTS `add_hits`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `add_hits`( In id varchar(255))
BEGIN
select hits into @hits from db_books where Book_ID = id;
update db_books set hits=@hits+1 where Book_ID = id;
END$$
PDO没有创建SP,那么如何才能完成此任务?
我尝试一起并逐行执行所有代码部分,但没有任何效果.
我正在尝试制作数据库安装程序脚本.
The PDO is not creating the SPs, how will be able to accomplish this task?
I have tried executing all the code part together and line by line, but nothing works.
I am trying to make a DB installer script.
推荐答案
PMA帮助我回答了我自己的问题.
为了克服这个问题,您需要删除过程的定界符部分,以使查询变为:
Well, PMA Helped me with answering this Question of my own.
To overcome this you need to remove the delimiter part of the procedure, so that your queries become like:
DROP PROCEDURE IF EXISTS `add_hits`;
CREATE DEFINER=`root`@`localhost` PROCEDURE `add_hits`( In id varchar(255))
BEGIN
declare hits_bk int;
select hits into hits_bk from db_books where Book_ID = id;
update db_books set hits=hits_bk+1 where Book_ID = id;
END;
现在查询将可用.
感谢@Your常识和@RiggsFolly的帮助.
Now the queries will work.
Thanks to @Your Common Sense and @RiggsFolly for helping out.
这篇关于在PHP中使用PDO创建存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!