下午好!致命錯誤:在Microsoft Access數據庫上使用PDO調用非成員函數fetchALL()
我已經建立了一個到Microsoft Access數據庫(工作)的連接類。然而,我的問題在於,我試圖用這個類來執行一個簡單的SQL語句,並且收到錯誤消息:致命錯誤:調用非對象上的成員函數fetchALL()。
我對PDO相當陌生,在網上閱讀了很多文章,但無濟於事。我想我理解了我的問題,但並不完全,請有人可以瞭解一些情況,並可能提供答案以解釋爲什麼我會收到錯誤消息?
connectionClass.php
class connection{
public $con;
private $dbName;
function __construct(){
$this->dbName = $_SERVER["DOCUMENT_ROOT"] . "\database\yakety1new.mdb";
}
function connect(){
$this->con = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$this->dbName; Uid=Admin; Pwd=;");
$this->con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
return $this->con;
}
}
if (!ini_get('display_errors')) {
ini_set('display_errors', '1');
}
testIndex.php
try{
include_once '\classes\connectionClass.php';
$con = new connection();
$pdoConnection = $con->connect();
$sql = $pdoConnection->prepare("SELECT * FROM celebs");
$result = $pdoConnection->exec($sql);
while ($row = $result->fetchALL(PDO::FETCH_ASSOC)) {
echo $row['firstname'];
echo $row['surname'];
}
} catch (Exception $e){
echo 'ERROR:'.$e->getMessage();
file_put_contents('connection.errors.txt', $e->getMessage().PHP_EOL,FILE_APPEND);
}
if (!ini_get('display_errors')) {
ini_set('display_errors', '1');
}
錯誤消息與這條線:
while ($row = $result->fetchALL(PDO::FETCH_ASSOC)) {
任何幫助非常感謝,謝謝!
謝謝!這是擺脫了錯誤,但是,沒有結果顯示,我收到此警告:....警告:PDO :: exec()期望參數1是字符串,在第22行給出的對象 - 這是 - $ result = $ pdoConnection-> exec($ sql); –
儘管這只是一個警告,但它仍然應該顯示我的sql語句的結果? –
查看最新的答案。 – Barmar