I'm currently learning php and mysql and am trying to build an authentication webpage where the user registers and is able to log in to a member protected page. The registration process works fine but for some reason I'm getting this error in my login execution script.
Fatal error: Call to a member function prepare() on a non-object in /homepages/8/d459264879/htdocs/tymbi_reg/login_exec.php on line 40
Line 40 is here if($stmt = $mysqli->prepare("SELECT * FROM member WHERE username=? AND password =?"))
I've been trying hard to find where the problem is without any success.
This is the code that I'm using in my login script
<?php
session_start();
require_once('connection.php');
$errmsg_arr = array();
$errflag = false;
$username = $_POST['username'];
$password = $_POST['password'];
if($username == '') {
$errmsg_arr[] = 'Username missing';
$errflag = true;
}
if($password == '') {
$errmsg_arr[] = 'Password missing';
$errflag = true;
}
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: index.php");
exit();
} else {
if($stmt = $mysqli->prepare("SELECT * FROM member WHERE username=? AND password =?"))
{
$stmt->bind_param("ss", $username, $password);
$stmt->execute();
$stmt->store_results();
if($stmt->num_rows > 0)
{
$SESSION['username'] = $username;
header("Location: home.php");
}
else
{
$error['alert'] = "Username or password are incorrect";
}
}
}
?>
Here's my connection.php code
<?php
$con=new mysqli("test","test","test","test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
And yes, I have replaced the test values with my details