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 selecting Email
and Password
, there is no Firstname
so you can't access it.
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:
SELECT FirstName, Email, Password FROM users WHERE Email = 'test@test.com AND Password = 'test' AND Enabled = 1
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!
You only fetch Email and Password not a FirstName.
So your query will be like:
$data = mysql_query("SELECT FirstName,Email, Password FROM users WHERE Email = 'test@test.com AND Password = 'test' AND Enabled = 1") or die(mysql_error());
精彩评论