7

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.

3 Answers 3

14

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.

1
  • 1
    Because PDO allows only one query per executions, you can do it with Timo Huovinen's Solution by executing it one-by-one, preferably in transaction. Commented May 6, 2019 at 3:35
10

PHP only allows you to execute one query at a time normally, so $$ delimiters are not necessary.

$pdo = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$pdo->exec('DROP PROCEDURE IF EXISTS `add_hits`');
$pdo->exec('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');
0

In an effort to keep on topic and answer the question

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 first occurance of $$ will terminate the DDL so

DROP PROCEDURE IF EXISTS `add_hits`$$

So I think it should be

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
$$
1
  • Now my Query is: DELIMITER $$ 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$$ I have another procedure after this starting from: DROP PROCEDURE IF EXISTS add_issues_num; I am running the whole part together by the exec method. But it still doesn't work.
    – echo_salik
    Commented Aug 23, 2013 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.