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

2023-05-07php开发问题
5

本文介绍了致命错误:在非对象上调用成员函数 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17