MySQL Stored Procedures and PHPMyAdmin

时间:2016-05-11 11:33:03

标签: php stored-procedures mysqli

I have created a procedure (with the PHPMyAdmin tool) which returns a table and two outputs. If I execute the procedure with the button "Execute" of PHPMyAdmin it sends this query:

SET @p0 =  '12';
SET @p1 =  '1';
CALL `getPhoneReview` (@p0 , @p1 , @p2 , @p3);
SELECT @p2 AS  `PhoneCount` , @p3 AS  `ReviewCount`;

Which returns what is expected (the table and the two outputs). However, if I copy that exact code and execute from the SQL section it only executes the last sentence:

CALL `getPhoneReview` (@p0 , @p1 , @p2 , @p3)

Which doesn't return anything.

It's happening as well while trying to retrieve the output values with PHP. I have this code:

$C = new MySQLi(...);
$Q = $C->query("CALL getPhoneReview(12, 1, @PhoneCount, @ReviewCount)");
/* Looping over $Q works fine */
$PhoneCount = $C->query("SELECT @PhoneCount AS PhC");
$PCO = $PhoneCount->fetch_object();
var_dump($PCO->PhC); /*  Call to a member function fetch_object() on boolean */

Could anybody help me?

1 个答案:

答案 0 :(得分:1)

您的代码存在两个问题。

  • 首先,你的代码中没有适当的mysqli错误报告,你只有PHP的通知,但不是来自mysql的真正错误。您可以阅读如何正确使用here
  • 其次,在调用存储过程之后,应该始终调用$C->next_result();为了让其他查询被执行。