【问题标题】:PDO fetch through PHP class variablePDO 通过 PHP 类变量获取
【发布时间】:2016-09-22 19:27:53
【问题描述】:

你能解释一下为什么这不起作用吗?我将 PDO 提取存储到 PHP 类变量中,然后尝试使用函数调用对其进行循环。

PHP 类

private $conn;
private $id;
private $data = array();

public function __construct($conn, $id) {
    $this->id = $id;
    $this->loadData();
}

private function loadData() {
    $sql = $this->conn->prepare("SELECT * FROM table WHERE id = :id");
    $sql->bindValue(':id', $this->id);
    $sql->execute();

    $this->data = $sql->fetch();
}

public function getData() {
    return $this->data();
}

脚本本身

$test = new test($pdo, "100101");

while ($row = $test->getData()) {
    echo $row['item'];
}

只要达到内存限制,就会有循环。查询应返回 ca。仅 20 行。

提前致谢!

【问题讨论】:

  • 参数id = :id和绑定orderIdbindValue(':orderId',
  • @Saty:谢谢,在我的示例中这是一个错字。真实代码中没有任何拼写错误。

标签: php class pdo while-loop fetch


【解决方案1】:
  1. fetch() 更改为fetchAll()
  2. 更改为foreach ($test->getData() as $row)

【讨论】:

    猜你喜欢
    • 2011-07-05
    • 2023-03-17
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 2019-04-03
    • 1970-01-01
    • 2017-05-08
    相关资源
    最近更新 更多