簡體   English   中英

通過PHP類變量獲取PDO

[英]PDO fetch through PHP class variable

您能解釋一下為什么這行不通嗎? 我將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行。

提前致謝!

  1. fetch()更改為fetchAll()
  2. 更改為foreach ($test->getData() as $row)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM