2014-11-01 30 views
1

因此,在這個網站上有相當多的這個確切問題的實例。調用一個非對象的成員函數fetchAll() -

但大多有一大堆其他東西wtih它們混合,如類,許多參數等

我有一些非常基本的代碼,沒有,真的,可能是因爲基本的,因爲它得到:

try { 

    $connection = new PDO("mysql:host=localhost;dbname=desertstormweb_mybb", $mysql_user, $mysql_pass); 

    $test = 'SELECT * from mybb_users'; 
    $statement = $connection->prepare($test); 
    $statement = $statement->execute(); 
    $result = $statement->fetchAll(); 
    print($result); 

} catch(PDOException $e) { 
    echo $e->getMessage(); 
} 

基本上,我只是想在一個表中返回行,現在我已經使用過PDO無數遍了,我從來沒有過這樣的問題。

我甚至開始引用其他腳本我已經在過去做,不能完全弄明白......

這是怎麼回事?

回答

1

不要指定執行,否則你會得到對象覆蓋:

$test = 'SELECT * from mybb_users'; 
$statement = $connection->prepare($test); 
$statement->execute(); // invoke the execute, but don't overwrite 
$result = $statement->fetchAll(); // PDO::FETCH_ASSOC 
+1

BRB,撲另一個洞口進入我的牆我的頭骨。我真的一直在擺弄這個基本的代碼兩個小時,這很簡單。謝謝,我會在幾分鐘內標記爲正確的。 – Hobbyist 2014-11-01 04:55:10

+0

@ Christian.tucker hehe:D是的,這些微妙的東西有時會破壞東西。確定男人沒有概率,很高興這有幫助 – Ghost 2014-11-01 04:58:07

+0

是的!現在我只需要弄清楚它爲什麼會返回一個空數組......今天不是我的一天,查詢在phpmyadmin,._上完美執行。 – Hobbyist 2014-11-01 05:04:03

0

http://php.net/manual/en/pdostatement.fetchall.php

$sth = $dbh->prepare("SELECT name, colour FROM fruit"); 
$sth->execute(); 

/* Fetch all of the remaining rows in the result set */ 
print("Fetch all of the remaining rows in the result set:\n"); 
$result = $sth->fetchAll(); 
print_r($result); 

http://php.net/manual/en/pdostatement.execute.php

PDOStatement對象::執行返回TRUE或FALSE的成功失敗。 因此,而不是有:

$statement = $statement->execute(); 

只需使用

$statement->execute(); 

OR

if ($statement->execute()) 
{ 
    $result = $statement->fetchAll(); 
    print($result); 
} 
相關問題