【发布时间】:2011-02-14 21:41:16
【问题描述】:
我的数据库中有一个存储过程,它返回表中的所有记录:
CREATE PROCEDURE showAll()
BEGIN
SELECT * FROM myTable;
END
SP 按预期工作。但是,如果我在 php 脚本中调用它,然后尝试再次查询数据库,它总是会失败:
// $mysqli is a db connection
// first query:
if (!$t = $mysqli->query("call showAll()"))
die('Error in the 1st query');
while ($r = $t->fetch_row()) {
echo $r[0] . "<br>"; // this is ok
}
$t->free(); // EDIT (this doesn't help anyway)
// second query (does the same thing):
if (!$t = $mysqli->query("SELECT * from myTable"))
die('Error in the 2nd query'); // I always get this error
while ($r = $t->fetch_row()) {
echo $r[0] . "<br>";
}
值得注意的是,如果我交换两个查询(即我最后调用存储过程),它可以正常工作而不会出现任何错误。在第二个查询之前关闭()结果没有帮助。 一些提示?
编辑:mysqli::error() 是:«命令不同步;您现在无法运行此命令»。
【问题讨论】:
-
如果你调用 $t->free(); 会发生什么在您处理完第一个查询之后?
-
你检查过
mysqli::error吗? -
我在第一个 while 循环之后放了一个 $t->free() 但行为没有改变。 mysqli::error 是:“命令不同步;您现在无法运行此命令”。
-
你为什么不直接使用
SELECT * FROM myTable;????? -
那个 SP 只是一个例子。实际情况更复杂,我更喜欢使用 SP 来保持数据完整性。
标签: php stored-procedures mysqli