php - 调用存储过程Mysql时命令不同步

标签 php stored-procedures mysqli

我有两个存储过程,我需要将记录分页(比如选择下一个 n 记录)到第一个选择所有匹配记录的记录。

CREATE PROCEDURE `trans_all`(IN varphone VARCHAR(15))
BEGIN
  Select
  loans.amt,
  loans.date,
  loans.pay_period,
  borrower.phone As borrower_phone,
  borrower.name As borrower_name,
  lender.phone As lender_phone,
  lender.name As lender_name,
From
  loans Left Join
  users borrower On borrower.id = loans.borrower_id Left Join
  users lender On lender.id = loans.lender_id
Where
   (lender.phone = varphone) or (borrower.phone = varphone);
END

然后我在 php 中得到计数;像这样

$result = $mysqli->query(sprintf("call trans_all('%s')",$phone));
$num_recs = $result->num_rows; 

然后我需要选择精确的记录才能执行此操作

$result = $mysqli->query(sprintf("call trans_history('%s','%s','%s')",$phone,$start,$limit));

$row = $result->fetch_assoc(); // this like gives the error Commands out of sync; you can't run this command now

第二个存储过程是

CREATE PROCEDURE `trans_history`(IN varphone VARCHAR(15), IN page INT, IN items INT)
BEGIN
  Select
  loans.amt,
  loans.date,
  loans.pay_period,
  borrower.phone As borrower_phone,
  borrower.name As borrower_name,
  lender.phone As lender_phone,
  lender.name As lender_name,
From
  loans Left Join
  users borrower On borrower.id = loans.borrower_id Left Join
  users lender On lender.id = loans.lender_id
Where
   (lender.phone = varphone) or (borrower.phone = varphone)  LIMIT page , items;
END

什么可能导致这个错误?

最佳答案

SP 返回包含状态的第二个结果集。在进行后续查询之前,您需要使用 next_result()

$result = $mysqli->query(sprintf("call trans_all('%s')",$phone));
$num_recs = $result->num_rows; 
$result->close();
$mysqli->next_result(); // <- you need this before you make another query

//Then make call second SP
$result = $mysqli->query(sprintf("call trans_history('%s','%s','%s')",$phone,$start,$limit));
$row = $result->fetch_assoc();
$result->close();
$mysqli->next_result();

关于php - 调用存储过程Mysql时命令不同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16683177/

相关文章:

php - SQL 从表中获取值

php - 将数据库中的多个数据分组并一起显示

php - Magento -> 我希望管理员中的产品网格显示在前端

sql - 您可以使用 CASE 语句来评估您传递给存储过程的参数吗?

javascript - jTable如何获取选定行数据

java - 使用SQL存储过程我们可以得到以下输出吗?或者在代码本身中处理它?

Mysql程序从文件插入数据

php - 从 PHP 传递时 SQL 返回错误

php - 使用嵌套无序列表构建树

javascript - 使用两个选择控件编辑表单,以便在首次启动时进行过滤