Php 调用存储过程Mysql时命令不同步
我有两个存储过程,我需要将记录分页(例如,选择下一个Php 调用存储过程Mysql时命令不同步,php,stored-procedures,mysqli,Php,Stored Procedures,Mysqli,我有两个存储过程,我需要将记录分页(例如,选择下一个nrecords)到选择所有匹配记录的第一个 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
n
records)到选择所有匹配记录的第一个
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
导致此错误的原因是什么?SPs返回第二个结果集,其中包含状态。在进行后续查询之前,需要使用
next\u 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();
$result=$mysqli->query(sprintf(“calltrans_all(“%s”),$phone));
$num\u recs=$result->num\u行;
$result->close();
$mysqli->next_result();//查询(sprintf(“呼叫传输历史(“%s”、““%s”、““%s”)”)、$phone、$start、$limit));
$row=$result->fetch_assoc();
$result->close();
$mysqli->next_result();