我添加了以下行:
$userquery = mysqli_real_escape_string ($db, $userquery);
在添加之前使用的代码,现在我收到以下2个错误:
mysqli_real_escape_string() expects parameter 2 to be string, object given
和
mysqli_num_rows() expects parameter 1 to be mysqli_result, null given
代码
$userquery = mysqli_query($db, "SELECT * FROM staff WHERE forename = '$forename'") or die ("error getting information from database.");
$userquery = mysqli_real_escape_string ($db, $userquery);
if (mysqli_num_rows($userquery) == null ){
die ("No staff directory found");
}
任何人都可以告诉我为什么我会收到这些错误以及我可以采取哪些措施来解决这些问题?
我理解第一个错误是因为$ userquery被使用了两次,但如果我不使用它两次,我怎么能使用mysqli_real_escape_string扩展?
我尝试更改第一个$ userquery的变量名,除去错误然后发生新错误:
mysqli_real_escape_string() expects parameter 2 to be string, object given
答案 0 :(得分:1)
$forename= mysqli_real_escape_string ($forename);
$userquery = mysqli_query($db, "SELECT * FROM staff WHERE forename = '$forename'");
$result= mysqli_fetch_object ($userquery);
if (mysqli_num_rows($result) == null ){
die ("No staff directory found");
}
首先取各种变量名称
你不能花费$ userquery时间和分配不同的值,所以采取diffvalue我采取$result
答案 1 :(得分:0)
您正在将mysqli_real_escape_string用于所有查询结果,并且打算与字符串一起使用。使用您的示例,您需要:
$forename_escaped = mysqli_real_escape_string ($db, forename);
$userquery = mysqli_query($db, "SELECT * FROM staff WHERE forename = '$forename_escaped'") or die ("error getting information from database.");
if (mysqli_num_rows($userquery) == null ){
//do stuff
}
答案 2 :(得分:0)
$forename = mysqli_real_escape_string ($db, $forename);
$sql = "SELECT * FROM staff WHERE forename = '$forename'";
$res = mysqli_query($db, $sql) or trigger_error ($db->error);
if (!mysqli_num_rows($res)){
die ("No staff directory found");
}
答案 3 :(得分:-1)
$userquery = mysqli_query($db, "SELECT * FROM staff WHERE forename = '$forename'") or die ("error getting information from database.");
$userquery = mysqli_real_escape_string ($userquery);
if (mysqli_num_rows($userquery) == 0){
die ("No staff directory found");
}
看
首先,
mysqli_real_escape_string()只是一个参数,它是mysqli_query的结果值。
secord,
mysqli_num_rows返回一个整数值,因此你不能使用NULL。
希望能给你帮助