警告:mysqli_query() 需要至少 2 个参数,其中 1 个在 [...]
Warning: mysqli_query() expects at least 2 parameters,1 given in [...]
- 警告:mysqli_query() 需要至少 2 个参数,其中 1 个在第 11 行的 C:\xampp\htdocs\Quman\registar.php
- 警告:mysqli_num_rows() 期望参数 1 为 mysqli_result,第 12 行 C:\xampp\htdocs\Quman\registar.php 中给出的空值
- 警告:mysqli_query() 需要至少 2 个参数,其中 1 个在第 23 行的 C:\xampp\htdocs\Quman\registar.php
- 警告:mysqli_error() 需要 1 个参数,0 在第 23 行的 C:\xampp\htdocs\Quman\registar.php
代码:
if (isset($_POST['criar'])) {
$nome = $_POST['nome'];
$apelido = $_POST['apelido'];
$email = $_POST['email'];
$pass = $_POST['pass'];
$data = date("Y/m/d");
$email_check = mysqli_query("SELECT email FROM users WHERE email='$email'");
$do_email_check = mysqli_num_rows($email_check);
if ($do_email_check >= 1) {
echo '<h3>Este email já está registado, faça o login, caso seja o seu, entre <a href="login.php">aqui!</a></h3>';
}elseif ($nome == '' OR strlen($nome)<3) {
echo '<h3>Escreva seu nome corretamente!</h3>';
}elseif ($email == '' OR strlen($email)<10) {
echo '<h3>Escreva seu email corretamente!</h3>';
}elseif ($pass == '' OR strlen($pass)<8) {
echo '<h3>Escreva a palavra-passe corretamente, deve possuir no mínino 8 caracteres!</h3>';
}else{
$query = "INSERT INTO users (`nome`,`apelido`,`email`,`password`,`data`) VALUES ('$nome','$apelido','$email','$pass','$data')";
$data = mysqli_query($query) or die(mysqli_error());
if ($data) {
setcookie("login",$email);
header("Location: ./");
}else{
echo "<h3>Desculpe, houve um erro ao registar-se...</h3>";
}
}
}
错误不言自明,mysqli_query
需要 2 个参数 - 文档可在此处获得:https://php.net/mysqli_query
根据文档,第一个参数是通过 mysqli_connect
创建的数据库连接。
您的内容应该类似于:
$db = mysqli_connect(..);
// ..
$email_check = mysqli_query($db, "SELECT email FROM users WHERE email='$email'");
此外,您的代码容易受到 SQL 注入攻击,您应该考虑在查询中使用参数,例如:
if ($stmt = mysqli_prepare($db, "SELECT email FROM users WHERE email = ?"))
{
mysqli_stmt_bind_param($stmt, "s", $email);
mysqli_stmt_execute($stmt);
$email_check = mysqli_stmt_num_rows($stmt);
}
TL;DR 您需要在 mysqli_query 函数中提供查询连接。
我想您是在问为什么会收到这些错误消息。第一条消息指定您需要在 mysqli_query 调用中有两个参数。第一个是与您的服务器的连接,第二个是查询本身。
这是来自 https://www.w3schools.com/php/func_mysqli_query.asp
的示例
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
mysqli_query($con,"SELECT * FROM Persons");
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)");
mysqli_close($con);
?>
第二个错误消息是因为当msqli_query调用失败时$email_check将变为空,因此使msqli_num_rows失败。
请注意,您显然必须更改所有 myqsli_query 调用。如果你想阅读更多关于调用的信息,这里是手册的 link,http://php.net/manual/en/mysqli.query.php。
- 警告:mysqli_query() 需要至少 2 个参数,其中 1 个在第 11 行的 C:\xampp\htdocs\Quman\registar.php
- 警告:mysqli_num_rows() 期望参数 1 为 mysqli_result,第 12 行 C:\xampp\htdocs\Quman\registar.php 中给出的空值
- 警告:mysqli_query() 需要至少 2 个参数,其中 1 个在第 23 行的 C:\xampp\htdocs\Quman\registar.php
- 警告:mysqli_error() 需要 1 个参数,0 在第 23 行的 C:\xampp\htdocs\Quman\registar.php
代码:
if (isset($_POST['criar'])) {
$nome = $_POST['nome'];
$apelido = $_POST['apelido'];
$email = $_POST['email'];
$pass = $_POST['pass'];
$data = date("Y/m/d");
$email_check = mysqli_query("SELECT email FROM users WHERE email='$email'");
$do_email_check = mysqli_num_rows($email_check);
if ($do_email_check >= 1) {
echo '<h3>Este email já está registado, faça o login, caso seja o seu, entre <a href="login.php">aqui!</a></h3>';
}elseif ($nome == '' OR strlen($nome)<3) {
echo '<h3>Escreva seu nome corretamente!</h3>';
}elseif ($email == '' OR strlen($email)<10) {
echo '<h3>Escreva seu email corretamente!</h3>';
}elseif ($pass == '' OR strlen($pass)<8) {
echo '<h3>Escreva a palavra-passe corretamente, deve possuir no mínino 8 caracteres!</h3>';
}else{
$query = "INSERT INTO users (`nome`,`apelido`,`email`,`password`,`data`) VALUES ('$nome','$apelido','$email','$pass','$data')";
$data = mysqli_query($query) or die(mysqli_error());
if ($data) {
setcookie("login",$email);
header("Location: ./");
}else{
echo "<h3>Desculpe, houve um erro ao registar-se...</h3>";
}
}
}
错误不言自明,mysqli_query
需要 2 个参数 - 文档可在此处获得:https://php.net/mysqli_query
根据文档,第一个参数是通过 mysqli_connect
创建的数据库连接。
您的内容应该类似于:
$db = mysqli_connect(..);
// ..
$email_check = mysqli_query($db, "SELECT email FROM users WHERE email='$email'");
此外,您的代码容易受到 SQL 注入攻击,您应该考虑在查询中使用参数,例如:
if ($stmt = mysqli_prepare($db, "SELECT email FROM users WHERE email = ?"))
{
mysqli_stmt_bind_param($stmt, "s", $email);
mysqli_stmt_execute($stmt);
$email_check = mysqli_stmt_num_rows($stmt);
}
TL;DR 您需要在 mysqli_query 函数中提供查询连接。
我想您是在问为什么会收到这些错误消息。第一条消息指定您需要在 mysqli_query 调用中有两个参数。第一个是与您的服务器的连接,第二个是查询本身。
这是来自 https://www.w3schools.com/php/func_mysqli_query.asp
的示例<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
mysqli_query($con,"SELECT * FROM Persons");
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)");
mysqli_close($con);
?>
第二个错误消息是因为当msqli_query调用失败时$email_check将变为空,因此使msqli_num_rows失败。 请注意,您显然必须更改所有 myqsli_query 调用。如果你想阅读更多关于调用的信息,这里是手册的 link,http://php.net/manual/en/mysqli.query.php。