Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

mysql - Create Stored Procedures with PDO in PHP

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$$

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;

Now the queries will work.
Thanks to @Your Common Sense and @RiggsFolly for helping out.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...