[英]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行。
提前致謝!
fetch()
更改為fetchAll()
foreach ($test->getData() as $row)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.