2013-05-22 83 views
3

我有兩個存儲過程,我需要將記錄分頁(比如說選擇每個下一個n記錄)到第一個選擇所有匹配記錄的記錄。命令在調用存儲過程時不同步命令Mysql

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 

什麼可能會導致此錯誤?

回答

6

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(); 
相關問題