PDO::FETCH_OBJECT
我的论点有问题。我想获取一个对象而不是一个数组,但是当我尝试这个时:
try {
$conn = new PDO('mysql:host=localhost;dbname=washngo', $config['DB_USERNAME'], $config['DB_PASSWORD']);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //Fetch errors by default ( display any errors during the development process )
$stmt = $conn->prepare('SELECT * FROM news');
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_OBJECT)) { //By default, it fetch an array. The "PDO::FETCH_OBJECT" argument allows us to fetch an object
print_r($row);
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
我明白了
致命错误:
FETCH_OBJECT
第 18 行 index.php 中未定义的类常量“”。
当我尝试让fetch()
默认情况下(不带PDO::FETCH_OBJECT()
)时,它工作正常。