我目前正在学习 php 和 mysql,并且正在尝试构建一个身份验证网页,用户可以在其中注册并能够登录到受成员保护的页面。注册过程运行良好,但由于某种原因,我的登录执行脚本中出现此错误。
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
第 40 行在这里 if($stmt = $mysqli->prepare("SELECT * FROM member WHERE username=? AND password =?"))
我一直在努力寻找问题所在,但没有成功。
这是我在登录脚本中使用的代码
<?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";
}
}
}
?>
这是我的 connection.php 代码
<?php
$con=new mysqli("test","test","test","test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
是的,我已经用我的详细信息替换了测试值