<?php
class DataObject{
public function GetObjetList(){
// Connect to the database server
$dbh = new PDO("mysql:host=localhost;dbname=bookdb", "webuser", "secret");
// Execute the query return 1200 register
$stmt = $dbh->query('SELECT sku, title FROM products ORDER BY title');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$sku = $row['sku'];
$title = $row['title'];
return something?? --> how to??
}
}
?>
问候!
答案 0 :(得分:2)
PDO已经有一个返回对象的获取模式。
将您的代码更改为:
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
答案 1 :(得分:-1)
class DataObject
{
public function __construct($pdo)
{
$this->db = $pdo;
}
public function GetObjetList()
{
$sql = 'SELECT sku, title FROM products ORDER BY title';
return $this->db->query($sql)->fetchAll(PDO::FETCH_OBJ);
}
}
答案 2 :(得分:-2)
在while之后放入返回,而不是在内部,使用FETCH_OBJ和数组:
$rows = array();
$rows = $stmt->fetchAll(PDO::FETCH_OBJ);
return $rows;