Create Stored Procedures with PDO in PHP(在 PHP 中使用 PDO 创建存储过程)
问题描述
我正在从 PHP 读取 TEXT 文件并尝试从中执行命令,例如创建数据库及其拥有的所有表和过程.我的代码创建了表,但没有创建文件中给出的存储过程.
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 Common Sense 和@RiggsFolly 的帮助.
Now the queries will work.
Thanks to @Your Common Sense and @RiggsFolly for helping out.
这篇关于在 PHP 中使用 PDO 创建存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 PHP 中使用 PDO 创建存储过程
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- PHP - if 语句中的倒序 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01