gpt4 book ai didi

php - PDO fetchAll 非对象

转载 作者:太空宇宙 更新时间:2023-11-03 12:07:45 24 4
gpt4 key购买 nike

为什么我收到错误 Fatal error: Call to a member function fetchAll()。我已经查看了具有相同错误的其他问题,但我的代码似乎很好,它似乎与 $this->$dbvar_dump 返回 object 有关(PDO)#2 (0) { }

index.php

<?php  
include('assets/misc/functions.php');

$cars = new Cars(connection());

$listAll = $cars->listCars();

var_dump($listCars);
?>

config.php

<?php

function connection()
{
try
{
$host = 'localhost';
$dbuser = 'jzmcond_pdo1';
$dbpass = '';
$dbname = 'jzmcond_pdo1';

$dbConnection = new PDO("mysql:host=" . $host . ";dbname=" . $dbname.";", $dbuser, $dbpass);

return $dbConnection;
} catch (PDOException $error)
{
echo $error->getMessage();
return FALSE;
}
}
?>

函数:

<?php

include('config.php');

class Cars{
protected $db;

public function __construct(PDO $db){
$this->db = $db;
}

public function listCars()
{
$query = $this->db->prepare("SELECT `id` `rego` `engineSize` `type` `colour` `year` `additionalFeatures` FROM `cars`")->execute();
//return $query->fetchAll();

var_dump($this->db); exit();
}
}

?>

最佳答案

您的查询语法错误,您没有输入逗号

改变

SELECT `id` `rego` `engineSize` `type` `colour` `year` `additionalFeatures` FROM `cars

SELECT `id`, `rego`, `engineSize`, `type`, `colour`, `year`, `additionalFeatures` FROM `cars`

在这种情况下你甚至不需要准备,试试这个:

public function listCars()
{
$query = "SELECT `id`, `rego`, `engineSize`, `type`, `colour`, `year`, `additionalFeatures` FROM `cars`";
$stmt = $this->db->query($query);
if (!$stmt) {
echo "\nPDO::errorInfo():\n";
print_r($this->db->errorInfo());
exit();
}
$rows = $stmt->fetchAll();

return $rows;
}

关于php - PDO fetchAll 非对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25696419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com