php 警告:mysqli_query() 期望参数 1 是给定的 mysqli 布尔值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22582208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:07:18  来源:igfitidea点击:

Warning: mysqli_query() expects parameter 1 to be mysqli boolean given

phpmysqlmysqli

提问by user3450515

After submitting something through my form I get the welcome part and then the mysql connection error because mysql is off, it goes away when I turn it on and then the boolean error. "Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\welcome.php on line 25"

通过我的表单提交一些东西后,我得到了欢迎部分,然后是 mysql 连接错误,因为 mysql 关闭,当我打开它时它消失了,然后是布尔错误。“警告:mysqli_query() 期望参数 1 是 mysqli,布尔值在 C:\xampp\htdocs\welcome.php 第 25 行中给出”

<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?><br>
Your password is <?php echo $_POST["password"]; ?><br>
You have purchased the <?php echo $_POST["sub_type"]; ?>
<?php
$mysqli_host = "localhost";
$mysql_username = "root";
$mysql_password = "123";
$site_db = "test";
$info_name = $_POST["name"];
$info_pass = $_POST["password"];
$info_email = $_POST["password"];
$sub_type = $_POST["sub_type"];

$con=mysqli_connect($mysqli_host,$mysql_username,$mysql_password,$site_db);
// Checks connection to twitch webpanel database and inserts registreation info
if (mysqli_connect_errno());
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

mysqli_query($con,"INSERT INTO Users (Username, Password, Email, Subcription)
VALUES ('$info_name', '$info_pass', '$info_email', '$sub_type')");

?>

</body>
</html>

回答by user4035

Most probably, you have a connection error in mysqli_connect. Wrong credentials or MySQL is down.

最有可能的是,您在 mysqli_connect 中有连接错误。凭据错误或 MySQL 已关闭。

if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    //you need to exit the script, if there is an error
    exit();
}

回答by kk497055

if (mysqli_connect_errno());
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit(); // **this is missing**
}
mysqli_query($con,"INSERT INTO Users (Username, Password, Email, Subcription)
VALUES ('$info_name', '$info_pass', '$info_email', '$sub_type')");

回答by Abhi Beckert

Here is your bug:

这是你的错误:

if (mysqli_connect_errno());

There should be no semicolon on the end.

最后不应该有分号。

Also according to the documentation you should be using this to check for connection errors:

另外根据文档,您应该使用它来检查连接错误:

if (mysqli_connect_error())

But as I said in a comment, I recommend using PDO instead of mysqli. And make sure you properly escape values inserted into the database and encrypt the password with pbkdf2 or scrypt.

但正如我在评论中所说,我建议使用 PDO 而不是 mysqli。并确保正确转义插入数据库的值并使用 pbkdf2 或 scrypt 加密密码。