PDO fetchAll非对象

时间:2014-09-06 03:11:40

标签: php mysql pdo

为什么我收到错误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();
    }
}

?>

2 个答案:

答案 0 :(得分:3)

您的查询语法错误,未插入逗号

更改

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;
}

答案 1 :(得分:0)

看看这是否有效。我从未按照你的方式做到这一点,但如果我要尝试,这就是我想要的......

<?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;
    }
}

<强>类

class Cars{
    protected $db;

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

    public function listCars()
    {
        // as @Meda suggested, commas in query, I never noticed that
        $query = $this->db->prepare("SELECT `id`,`rego`,`engineSize`,`type`,`colour`,`year`,`additionalFeatures` FROM `cars`");
        $query->execute();

        if($query->rowCount() > 0) {

                while($result = $query->fetch(PDO::FETCH_ASSOC)) {
                        $_array[]   =   $result;
                    }
            }
        return  ($query->rowCount() > 0)? $_array:0;
    //  return  ($query->rowCount() > 0)? $query->fetchAll():0;
    }
}

<强>用法

include('config.php');
$db = connection();

 if($db !== false) {
      $CarDude = new Cars($db);
      $_list = $CarDude->listCars();
      print_r($_list);
     } ?>