PDO :: FETCH_OBJECT错误

问题描述 投票:4回答:1

我对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())时,它工作正常。

php pdo
1个回答
8
投票

正确的不是PDO::FETCH_OBJECT而是PDO::FETCH_OBJ

© www.soinside.com 2019 - 2024. All rights reserved.