我刚回到一个月前工作正常的项目。今天我发现我得到一个“警告:mysqli :: query():无法获取mysqli”错误,我不明白为什么。我试过更改if语句,但我仍然得到相同的结果。
php页面
<?php
include_once 'db_connect.php';
include_once 'functions.php';
sec_session_start();
error_reporting(E_ALL); ini_set('display_errors', 1);
$email = $_SESSION['email'];
$name = $_SESSION['name'];
$hash = $_SESSION['hash'];
$password = $hash;
echo $name;
echo '<br>';
echo $email;
echo '<br>';
echo $hash;
echo '<br>';
$sql = "INSERT INTO signed_up(`name`, `email`, `password`) VALUES ('$name', '$email', '$password')";
if ($mysqli->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $mysqli->error;
}
//if ($result = $mysqli->query("INSERT INTO `signed_up`(`name`, `email`, `password`) VALUES ('$name', '$email', '$password')")){
//
//header('location: ../register_success.php');
//// $result->close();
//
//}
//else {
// echo "error";
//}
?>
错误即将
您已成功连接到数据库
gptgeoff
up666315@myport.ac.uk
$ 2Y $ 10 $瑞/ eSNwbTnM4ZyWetEL6.ELSepLPKLkEiJdi4WYMyp / sEEAk9hKe
警告:mysqli :: query():无法在第24行的/home/sites/unitest.co.uk/public_html/includes/register_user.inc.php中获取mysqli警告:main():无法第27行/home/sites/unitest.co.uk/public_html/includes/register_user.inc.php中获取mysqli错误:INSERT INTO signed_up(name
,email
,password
) VALUES('gptgeoff','up666315@myport.ac.uk','$ 2y $ 10 $ JUi / eSNwbTnM4ZyWetEL6.ELSepLPKLkEiJdi4WYMyp / sEEAk9hKe')
db_connect.php文件有效,因为我正在显示“你已经成功连接到数据库”,会话变量正在echo语句和$ sql查询的值中传递和回显,所以我无法理解为什么这已停止工作,请有人建议
db_connect.php
<?php
include_once 'psl-config.php'; // As functions.php is not included
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
if(!$mysqli) {
die('error connecting to database');
}
echo 'you have succesfuly connected to the database'.'<br/><br/>';
?>
答案 0 :(得分:0)
此行:
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
创建一个对象。它是类mysqli
的实例。一个对象将永远是真实的。您的if语句检查$mysqli
是否为假。这个if语句是错误的,因为连接成功与否该对象永远不会为假。
请勿手动检查连接错误。您的连接失败,PHP发出警告。请检查您的PHP配置是否有错误报告,因为您可能已将警告静音。还请启用mysqli错误报告。在创建mysqli实例之前插入此行。
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);