I prefer use $obj->value than $array['value'], so i used
$database->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
The problem is when i do a simple req :
public function getUsers()
{
return $this->conn->query('SELECT * FROM users')->fetch();
}
It return me a stdClass and i can't get users list. (Or i don't know how, but it look weird, I never saw stdClass before).
So with FETCH_OBJ attribute I've got:
$list = $users->getUsers();
var_dump($list); // object(stdClass)#4 (6)[...]
Without FETCH_OBJ attribute I've got: (With same code)
array(12)[...]
And I have 2 rows 6 cols so this line give me error: (not error, but not what I want)
$database->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
Do you know why ? Is there another way to set FETCH_OBJ as default ?
I just wanna make fetch() instead _fetch(PDO::FETCH_OBJ)_...
Thanks.
Edit: I don't own database class. I'm doing like this: database.php
try {
$database = new \PDO('mysql:host=localhost;dbname=projet', 'root', '');
$database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$database->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
} catch(PDOExeption $e) {
die ("error mysql: $e");
}
UserManager class :
class UserManager
{
private $conn;
public function __construct(\PDO $database)
{
$this->conn = $database;
}
public function getUsers()
{
return $this->conn->query('SELECT * FROM users')->fetch();
}
}
And in any file i want to use getUsers():
$users = new UserManager($database);
$list = $users->getUsers();