致命错误:使用Microsoft Access数据库上的PDO调用非对象上的成员函数fetchALL() [英] Fatal error: Call to a member function fetchALL() on a non-object - using PDO on a Microsoft Access Database

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

问题描述

下午好!

我建立了一个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"] . "\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

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)) {

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

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)) {

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆