2014-03-24 46 views
-1

我有一個PHP函數叫做getNumRows

function getNumRows() { 
    $query = $this->mysqli->prepare("CALL GetNumRows('$this->tableName')") or die('Unable to prepare: ' . $this->mysqli->error); 
    $query->execute(); 
    $query->store_result(); 
    $query->bind_result($rowCount); 
    while ($query->fetch()) { 
     $numRows = $rowCount; 
    } 
    $query->close(); 

    return $numRows; 
} 

它使用存儲過程CALL GetNumRows('TableName')

DROP PROCEDURE gpstrack.GetNumRows; 
CREATE DEFINER=`******`@`localhost` PROCEDURE `GetNumRows`(IN tab_name VARCHAR(40)) 
BEGIN 
SET @t1 = CONCAT( 'SELECT COUNT(*) FROM ', tab_name) ; 
PREPARE stmt3 FROM @t1 ; 
EXECUTE stmt3; 
DEALLOCATE PREPARE stmt3; 
END; 

但在行失敗我的功能#1:

Unable to prepare: Commands out of sync; you can't run this command now

我如何使用這個PROC獲得任何表的行數?

+0

可能重複[?mysqli的奉獻「命令不同步」的錯誤 - 爲什麼(http://stackoverflow.com/questions/3632075/mysqli-giving-commands-out-of-sync-error-why) –

+0

看起來很不一樣,因爲我在Prepare行中出現錯誤。 – Pachonk

+0

爲什麼你正在使用存儲過程進行這樣的小問題?爲什麼你讓SQL注入不安全? –

回答

0

我能夠使用此PHP解決它:

function getNumRows() 
{ 
    $query = $this->mysqli->prepare("CALL GetNumRows('$this->tableName')") or die('Unable to prepare: ' . $this->mysqli->error); 

    $query->execute(); 
    $query->store_result(); 
    $query->bind_result($rowCount); 
    $query->fetch(); 
    $numRows = $rowCount; 
    $this->mysqli->next_result();// ADDED THIS LINE 
    $query->close(); 

    return $numRows; 
} 
相關問題