如何从PHP代码中调用MySQL存储过程? [英] How to call a MySQL stored procedure from within PHP code?
本文介绍了如何从PHP代码中调用MySQL存储过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经在MySQL中创建了存储过程,并希望PHP调用该存储过程.最好的方法是什么?
I have stored procedure that I created in MySQL and want PHP to call that stored procedure. What is the best way to do this?
-MySQL客户端版本:4.1.11
-MySQL服务器版本:5.0.45
-MySQL client version: 4.1.11
-MySQL Server version: 5.0.45
这是我的存储过程:
DELIMITER $$
DROP FUNCTION IF EXISTS `getNodeName` $$
CREATE FUNCTION `getTreeNodeName`(`nid` int) RETURNS varchar(25) CHARSET utf8
BEGIN
DECLARE nodeName varchar(25);
SELECT name into nodeName FROM tree
WHERE id = nid;
RETURN nodeName;
END $$
DELIMITER ;
调用过程getTreeNodeName的PHP代码是什么?
What is the PHP code to invoke the procedure getTreeNodeName?
推荐答案
我现在通过使用mysqli
而不是mysql
找到了解决方案.
I now found solution by using mysqli
instead of mysql
.
<?php
// enable error reporting
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
//connect to database
$connection = mysqli_connect("hostname", "user", "password", "db", "port");
//run the store proc
$result = mysqli_query($connection, "CALL StoreProcName");
//loop the result set
while ($row = mysqli_fetch_array($result)){
echo $row[0] . " - " . + $row[1];
}
我发现许多人似乎在使用mysql_connect, mysql_query and mysql_fetch_array
时遇到问题.
I found that many people seem to have a problem with using mysql_connect, mysql_query and mysql_fetch_array
.
这篇关于如何从PHP代码中调用MySQL存储过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文