Fatal error: Call to a member function fetchALL() on a non-object - using PDO on a Microsoft Access Database

后端 未结 2 1716
执念已碎
执念已碎 2020-12-20 10:05

Good Afternoon!

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

相关标签:
2条回答
  • 2020-12-20 10:36
    ini_set('display_errors', '1');
    include_once '\classes\connectionClass.php';
    $con = new connection();
    $pdoConnection = $con->connect();
    
    $data = $pdoConnection->query("SELECT * FROM celebs")->fetchAll();
    foreach ($data as $row) {
       echo $row['firstname'];
       echo $row['surname'];
    }
    

    this is all the code you need.

    0 讨论(0)
  • 2020-12-20 10:50

    $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();
    

    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)) {
    
    0 讨论(0)
提交回复
热议问题