2012-11-08 66 views
1

我使用這個說法在我的分貝如何在php中使用fetchAll?

$result = $stmt->fetchAll(PDO::FETCH_COLUMN, 0); 

獲取一列中的元素,但試圖運行它時,我得到這個錯誤。

調用未定義的方法mysqli_stmt::fetchAll();

有什麼我需要添加到我的PHP文件,包括方法還是什麼?

更新1:

class LoginAPI 
{ 
private $db; 

function __construct() 
{ 
    $this->db = new mysqli('xxx', 'xxx', 'xxx', 'xxx'); 
    $this->db->autocommit(FALSE); 
} 

function __destruct() 
{ 
    $this->db->close(); 
} 

function login() 
{ 
    if(isset($_POST["lang"])) 
    { 
     $stmt = $this->db->prepare("SELECT code FROM locations"); 
     $stmt->execute(); 
     $stmt->bind_result($code); 

     //while($stmt->fetch()) 
     //{ 
     // result[] = "code"=>$code; 
     // break; 
     //} 
     $codeArr = array(); 
     $result = $stmt->fetch_all();//|PDO::FETCH_GROUP; 




     sendResponse(200, json_encode($result)); 
     return true; 
    } 
    sendResponse(400, 'Invalid Request'); 
    return false; 
} 
} 
+1

你正在使用'PDO'或'mysqli'?無論如何,你在這裏混合兩個。 – itachi

+2

看起來像你的混合PDO和mysqli。如果你發佈了連接方式,它會顯示你正在創建一個mysqli連接對象,而不是PDO語句。 mysqli結果集對象具有fetch_all()而不是函數fetchAll()。 fetchAll()是PDOStatement類提供的一個函數。看到這個:http://php.net/manual/en/mysqli-result.fetch-all.php – Ray

+0

請檢查http://us1.php.net/pdostatement.fetchall#refsect1-pdostatement.fetchall-examples – CodeTalk

回答

5

您使用的mysqli,並且你已經有了一個說法不是一個結果。如果你有一個mysqli_result,你可以使用方法fetch_all()

但是,由於這是一個mysqli_stmt,您首先需要對結果集執行然後使用fetch_all()。看下面的例子

$stmt = $this->db->prepare("SELECT code FROM locations"); 
$stmt->execute(); 


//grab a result set 
$resultSet = $stmt->get_result(); 

//pull all results as an associative array 
$result = $resultSet->fetch_all(); 

注意:與fetch_all你不需要綁定結果作爲你的原代碼。

+0

我將其更改爲fetch_all(),現在我得到那種方法的錯誤。我仍然對所有這些都很陌生 – linuxer

+0

@linuxer你可以發佈你看到的確切的新錯誤 – Ray

+0

@linuxer你也使用php 5.3或更早版本 – Ray

相關問題