Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO::FETCH_OBJECT error

Tags:

php

pdo

I have a problem with the PDO::FETCH_OBJECT argument. I want to fetch an object and not an array, but when I try this:

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();
}

I get

Fatal error: Undefined class constant 'FETCH_OBJECT' in index.php on line 18.

When I try to let the fetch() by default (without PDO::FETCH_OBJECT()), it works fine.

like image 517
Chuck Avatar asked Feb 20 '23 07:02

Chuck


1 Answers

Correct is not PDO::FETCH_OBJECT but PDO::FETCH_OBJ

like image 128
s.webbandit Avatar answered Mar 05 '23 19:03

s.webbandit