我对这个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();
}
Run Code Online (Sandbox Code Playgroud)
我明白了
致命错误:
FETCH_OBJECT第18行的index.php中未定义的类常量' '.
当我尝试fetch()默认(没有PDO::FETCH_OBJECT())时,它工作正常.