【问题标题】:Uncaught Error: Call to undefined method DB::execute()未捕获的错误:调用未定义的方法 DB::execute()
【发布时间】:2017-05-19 05:42:03
【问题描述】:

我正在使用我在 GitHub 上找到的indieteq-php-my-sql-pdo-database-class

我创建了一个小页面来尝试显示一些数据库信息,如下所示,但是我在下面的测试页面脚本下方收到错误;

我的测试页

<?php

require("Db.class.php");
$db = new Db();
$db->query("SELECT * FROM faction_territories");

if ($db->execute()) {
    while ($row = $db->fetch(PDO::FETCH_ASSOC)) {
        echo '<tr>
            <td>'.$row['territory_id'].'</td>
            <td>'.$row['name'].'</td>
            <td>'.$row['density'].'</td>
            <td>'.$row['sector'].'</td>
            <td>'.$row['size'].'</td>
            <td>'.$row['respect'].'</td>
            <td>'.$row['faction_name'].'</td>
        </tr>';
    }
    echo '</table>';
}

?>

错误

致命错误:未捕获的错误:调用未定义的方法 DB::execute() in /var/www/.../index.php:7 堆栈跟踪:#0 {main} 抛出 /var/www/.../index.php 在第 7 行

问题

如您所知,我对 PHP 和 PDO 有点陌生。任何示例代码都可以很好地指出我出错的地方。

【问题讨论】:

  • 但是Db 类中确实没有execute() 方法。我觉得很清楚
  • @DanMiller 请问我该如何显示我的数据?我需要添加execute() 方法吗?
  • sources 它返回fetchAll() 所以你有结果作为数组。
  • @DanMiller 所以$db 变成了一个数组?如果我尝试回显 $db - Object of class DB could not be converted to string,则会收到以下错误
  • 没问题。没关系,已经给出了正确答案。您可以接受现有的。

标签: php pdo


【解决方案1】:

$db->查询已经执行了查询。您只需要遍历结果。

请看这里:https://github.com/wickyaswal/indieteq-php-my-sql-pdo-database-class/blob/master/Db.class.php#L189

【讨论】:

    猜你喜欢
    • 2021-04-17
    • 1970-01-01
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 2017-06-11
    相关资源
    最近更新 更多