下面是mysql查询,问题是只要只有一个php
变量,例如
Select * From student where Account = ".$username."
但如果我将代码更改为如下所示,则会收到错误
警告:mysqli_num_rows()期望参数1为mysqli_result,给定布尔值。
这可能是什么问题,请帮助我。
$result = $con->query("SELECT * FROM '.$role.' WHERE Account = '.$username.' AND password = '.$password.' AND IsApproved = '.$approval.'");
if(mysqli_num_rows($result) == 1) {
$_SESSION['username'] = $username;
header('Location: include/test.php');
}
else {
echo "Account is invalid";
}
}
编辑:问题已解决 - 由Dhaval Dave回答
$result = $con->query("SELECT * FROM {$role} WHERE Account = '{$username}' AND password = '{$password}' AND IsApproved = '{$approval}'");
非常感谢大家帮助我提出所有建议,我很感激你们对我们的所作所为。非常感谢x1000
答案 0 :(得分:-1)
$result = $con->query("SELECT * FROM ".$role." WHERE Account = ".$username." AND password = ".$password." AND IsApproved = ".$approval.");
答案 1 :(得分:-1)
请尝试
<?php
$result = $con->query('SELECT * FROM "'.$role.'" WHERE Account = "'.$username.'" AND password = "'.$password.'" AND IsApproved = "'.$approval'"');
答案 2 :(得分:-1)
在sql条件中传递字符串时,需要传递单引号。
$result = $con->query("Select * From student where Account = '".$username."' AND password = '".$password."' ");
答案 3 :(得分:-1)
试试这个,
$role = trim($role);
$result = $con->query("SELECT * FROM {$role} WHERE Account = '{$username}' AND password = '{$password}' AND IsApproved = '{$approval}'");