Fatal error: Call to a member function fetchALL() on a non-object - using PDO on a Microsoft Access Database(致命错误:在非对象上调用成员函数 fetchALL() - 在 Microsoft Access 数据库上使用 PDO)
问题描述
下午好!
我已经建立了一个到 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:致命错误:在非对象上调用成员函数 fetchALL() - 在 Microsoft Access 数据库上使用 PDO
基础教程推荐
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何替换eregi() 2022-01-01
