So I am new to PHP and particularly the world of PDO. I am using PDO to select some data from a table and create an object out of this data. I have been successful with PDO creating a default object and accessing the results. This is the fetch statement for fetching a default object, which works as expected:
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_OBJ);
The next thing I want to do is fetch the results and create a custom object out of a predefined class that I have created. I based the below statement on what I was using for the first statement, however, this does not work
$stmt->execute();
$user_object = $stmt->fetch(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'classname');
I have got it working by setting the fetch mode outside of the fetch method and can be seen here:
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_CLASS| PDO::FETCH_PROPS_LATE,'classname');
$object = $stmt->fetch();
I have consulted the PHP manuals but I do not understand why I cannot have a statement like the second one i.e. set the fetchmode within the fetch statement.
Thanks
Update
The following statement:
$user_object = $stmt->fetch(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'classname');
var_dump($stmt->errorInfo());
var_dump($user_object);
Returns -
array(3) { [0]=> string(5) "00000" [1]=> NULL [2]=> NULL } bool(false)
fetch()returnfalse? Or did it return an object but not as expected?var_dump($object)it returns abool(false)indicating that my object hasn't been createderrorInfo()method shed any light?