简体   繁体   English

mysql存储过程不起作用

[英]mysql stored procedures not working

I am trying to call a mysql stored procedure using the following code: 我正在尝试使用以下代码调用mysql存储过程:

 $mysqli = new mysqli("localhost", "user", "password", "mydatabase");
 $result = $mysqli->query('CALL storedproc_allusers()');

 while($row = $result->fetch_array(MYSQLI_BOTH))
 {
 echo $row['email'] . "<br />";
 }

The stored procedure is: 存储过程为:

 USE `mydatabase`$$
 DROP PROCEDURE IF EXISTS `storedproc_allusers`$$
 CREATE DEFINER=`user`@`xx.xxx.xx.xx` PROCEDURE `storedproc_allusers`()
 BEGIN
 SELECT * FROM users;
 END$$
 DELIMITER ;

I know my connection to the db is good because if I run a simple select query I get what I expect. 我知道我与数据库的连接很好,因为如果我运行一个简单的选择查询,我会得到期望的结果。

I get the following error when I run the above: 运行以上命令时出现以下错误:

Fatal error: Call to a member function fetch_array() on a non-object etc. etc. which leads me to think nothing is being returned from the query. 致命错误:在非对象等上调用成员函数fetch_array()等,这使我认为查询没有返回任何内容。

I use SQLYOG and can successfully run the procedure from there. 我使用SQLYOG,可以从那里成功运行该过程。 Any help would be appreciated. 任何帮助,将不胜感激。

You can find multiple examples here http://php.net/manual/en/mysqli.quickstart.stored-procedures.php . 您可以在http://php.net/manual/en/mysqli.quickstart.stored-procedures.php上找到多个示例。

$mysqli = new mysqli("localhost", "user", "password", "mydatabase");
$mysqli->query('CALL storedproc_allusers()');
do {
    if ($result = $mysqli->store_result()) {
        var_dump($result->fetch_all());
        $result->free();
    }
} while ($mysqli->more_results() && $mysqli->next_result()) ;

Try this 尝试这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM