This question already has an answer here:
This is what the error says
Fatal error: Call to a member function fetch() on a non-object in C:\xampp\htdocs\Grading\logging.php on line 30
I don't know what to write there im kinda new in mysqli and here is my code
<?php
$username = $_POST['username'];
$password = $_POST['password'];
$sql = $con->query("SELECT * FROM user WHERE username = '".$username."' ");
$row = $sql->fetch();
if ($username == $row['username'] and $password == $row['password'])
{
echo "<script>window.location.href='home.php'; </script>";
}
else
{
echo "<script> alert ('Invalid Username or Password'); window.location.href='login.php'; </script>";
}
?>
</div>
First of all, what is your $con variable? Guess its correct MySQL connection through MySQLi. So, you executed method query() of MySQLi library. But according to documentation of that method, you can see that result of execution is false, true or mysql_result.
And, what is much more important, mysqli_result does not have method fetch(). As you can see in docs, there're fetch_all(), fetch_assoc() and fetch_array() functions available. You can use them instead of fetch().
Moreover, fetch method is method of class mysqli_stmt (MySQLi Statement). But MySQLi prepared statement can be retrieved with $con->prepare() call, and not with $con->query() method.