I have been recently learning some OOP PHP and seem to have run into a problem when using the mysql_fetch_object(), receiving the error
Notice: Undefined property:
stdClass::$FirstName in
C:\xampp\htdocs\includes\login.php on
line 10
Here is the code I am using:
class User
{
public function CheckLogin()
{
$conn = new db();
$_email = mysql_escape_string($this -> Email);
$_password = mysql_escape_string($this -> Password);
$data = mysql_query("SELECT Email, Password FROM users WHERE Email = 'test@test.com AND Password = 'test' AND Enabled = 1") or die(mysql_error());
return mysql_fetch_object($data);
}
}
And I am calling this function as follows:
$u = new User();
$user = $u -> CheckLogin();
echo $user -> FirstName;
I’m pretty stumped by what I’m doing wrong, would anyone be able to point me in the right direction or give a possible fix?
You are fetching just two columns with your query: Email and Password. So all the other fields in your table, including the field FirstName, will not be fetched and is not available in the object your returning from the function.
Try the query:
This will also fetch the FirstName property, or use * to select all columns so you can use any field in your fetched object. Good luck!