致命错误:在非对象上调用成员函数 fetchALL() - 在 Microsoft Access 数据库上使用 PDO

时间:2023-05-07
本文介绍了致命错误:在非对象上调用成员函数 fetchALL() - 在 Microsoft Access 数据库上使用 PDO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

下午好!

我已经建立了一个到 Microsoft Access 数据库(有效)的连接类.然而,我的问题在于我试图使用这个类来执行一个简单的 SQL 语句,但我收到错误消息:致命错误:调用非对象上的成员函数 fetchALL().

I have made a connection class to a Microsoft Access Database (which works). However my problem lies where I'm trying to use this class to execute a simple SQL statement and I receive the error message: Fatal error: Call to a member function fetchALL() on a non-object.

我对 PDO 还很陌生,在网上阅读了很多文章,但都无济于事.我想我理解我的问题,但并不完全理解,请有人能解释一下这种情况,并可能就我收到错误消息的原因提供答案吗?

I'm fairly new to PDO and have read a lot of articles online but to no avail. I think I understand my problem but not fully, please could someone shed some light on the situation and possibly provide an answer to why i'm getting the error message?

connectionClass.php

connectionClass.php

class connection{

      public $con;
      private $dbName;

      function __construct(){
          $this->dbName = $_SERVER["DOCUMENT_ROOT"] . "databaseyakety1new.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

testIndex.php

try{
   include_once 'classesconnectionClass.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');
      }

错误信息与这一行有关:

The error message is related to this line:

while ($row = $result->fetchALL(PDO::FETCH_ASSOC)) {

非常感谢任何帮助,谢谢!

Any help is greatly appreciated, thanks!

推荐答案

$result 只是一个布尔值,表示查询是否成功.fetchAll 方法在 PDOStatement 上,所以它应该是:

$result is just a boolean that indicates whether the query was successful or not. The fetchAll method is on PDOStatement, so it should be:

while ($row = $sql->fetch(PDO::FETCH_ASSOC)) {

您也执行错误的语句,应该是:

You're also executing the statement wrong, it should be:

$result = $sql->execute();

您使用的方法是在不事先准备的情况下执行 SQL 字符串.你可以这样做:

The method you used is for executing a SQL string without first preparing it. You could instead do:

$result = $pdoConnection->exec("SELECT * FROM celebs");
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {

这篇关于致命错误:在非对象上调用成员函数 fetchALL() - 在 Microsoft Access 数据库上使用 PDO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何检查邮箱是否已经注册? 下一篇:PHP PDO SQL 只返回一行数据而不是所有行

相关文章