PHP login application. ERROR: Every time I run this it gives me this error, line
ID: 3902228 • Letter: P
Question
PHP login application.
ERROR: Every time I run this it gives me this error, line 42 is in black so you can see it. Any help is appreciated!!! thanks!!
Notice: Trying to get property 'num_rows' of non-object in /opt/lampp/htdocs/Project/login.php on line 42
$conn = new mysqli('localhost', 'root', '');
if($conn === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$cookie_name = '';
if(isset($_COOKIE[$cookie_name]))
{
echo "Welcome to our site";
echo "
";
echo " Logout ";
}
$show_form = false;
$login_result = $usernameErr = $passwordErr = '' ;
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(isset($_POST["submit"])){
extract($_POST);
if(empty($username)){
$usernameErr = "Please fill out username";
$show_form = true;
}
if(empty($password)){
$passwordErr = "Please fill out the password";
$show_form = true;
}
if( (!empty($username)) && (!empty($password)) && (!$show_form)){
$password = md5($password);
$search = " SELECT * FROM `login` WHERE `username` = '$username' AND `password` = '$password' ";
$result = $conn->query($search);
if($result->num_rows == 1){
$row = $result->fetch_assoc();
echo "Welcome to our site";
echo "
";
echo " Logout ";
if(isset($remember_me))
{
$cookie_name = "user";
$cookie_value = $row['username'];
setcookie($cookie_name, $cookie_value, time() + (86400 * 7), "/");
}
$conn->close();
}
else{
echo "Invalid credentails"; $show_form = true;}
} else{
$login_result = "Invalid credentials" ;
$show_form = true;
}
}
}
else {
$show_form = true;
}
if($show_form){
?>
}
?>
Username:Password:
Remember me
Explanation / Answer
If you have any doubts, please give me comment...
This notice occurs when you're using a non-object variable as an object. You're probably using : $a -> b like syntax on a variable $a which is not an object.
Make sure $result is object which have properties you're using.
Edit :
If the error is here $result->num_rows > 0. This means $result is not an object that have num_rows properties. Maybe it's NULL. Check its value before the test.
$search = " SELECT * FROM `login` WHERE `username` = '$username' AND `password` = '$password' ";
$result = $conn->query($search);
if(!$result){
//statements
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.