我对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();
}
我明白了
致命错误:第18行index.php中未定义的类常量'
FETCH_OBJECT
'。
当我试图默认让fetch()
(没有PDO::FETCH_OBJECT()
)时,它工作正常。
正确的不是PDO::FETCH_OBJECT
而是PDO::FETCH_OBJ