I have this small code:
$user = Database::getInstance()->query("SELECT username FROM users"); if ($user->count()) { foreach ($user as $users) { echo $users->username; } } var_dump($users);die(); This gives me an error:
Notice: Trying to get property 'username' of non-object in C:\xampp\htdocs\finaltask\test.php on line 8 array(2) { [0]=> object(stdClass)#5 (1) { ["username"]=> string(10) "reinisk157" } [1]=> object(stdClass)#6 (1) { ["username"]=> string(9) "reinisk22" } } If I understand correctly, Im trying to retrieve an object from an array, but I have no idea how else to get this data out of my DB. Please let me know if you need more details.
user$usersis probably not the one you want to loop over or echo from. You could change the db result from$userto$usersand then change your echo toecho $user->username. Right now you are getting the$usersout of nowhere.var_dump($user);