arning:mysqli_num_rows()期望参数1为mysqli_result,给定布尔值

时间:2016-01-31 10:04:14

标签: php mysqli

$value1 = $_POST['user'];
$dbc =mysqli_query($conn,"SELECT * FROM userdetails where (usernamename = '$value1') ");
if (mysqli_num_rows($dbc)>0)
    echo "success";
else
    echo "duplicate entry";

这是我的代码。但它返回

  

警告:mysqli_num_rows()期望参数1为mysqli_result,   第17行/home/u415962859/public_html/signupexe.php中给出的布尔值   重复条目

即使不是,也会显示重复的条目。请帮忙

6 个答案:

答案 0 :(得分:2)

检查您的查询是否失败。

$value1 = $_POST['user'];
$dbc =mysqli_query($conn,"SELECT * FROM userdetails where (usernamename = '$value1') ");
if (is_resource($dbc) and mysqli_num_rows($dbc)>0)
// OR
// if ($dbc !== false and mysqli_num_rows($dbc) > 0)
// I used !== instead of != as I consider it to be a good practice when checking
// values that are expected to have multiple data types, not only boolean.
    echo "success";
else
    echo "duplicate entry";

答案 1 :(得分:1)

试试这个:

$value1 = $_POST['user'];

$dbc =mysqli_query($conn,"SELECT * FROM userdetails where username = '$value1' ");

if (mysqli_num_rows($dbc)>0){

echo "success";

  } else {

echo "duplicate entry";

}

答案 2 :(得分:0)

如果查询失败,mysqli_query将返回false而不是结果。这就是出现此警告的原因。

由于mysqli_num_rows失败,它也返回false并执行else-block。

在将错误提供给其他命令之前,检查mysqli_query的结果是否有错误:

$value1 = $_POST['user'];
$dbc =mysqli_query($conn,"SELECT * FROM userdetails where (usernamename = '$value1') ");

// Check result from mysqli_query
if(!$dbc) die('mysqli_query error: ' . mysqli_error($conn));

if (mysqli_num_rows($dbc)>0)
    echo "success";
else
    echo "duplicate entry";

答案 3 :(得分:0)

如果a=raw_input("Enter the name of the School") l=list() for i in a: l.append(i) print l z=0 empty=" " for i in l: print for j in range(0,z+1): kl=l(j) print empty+j, z+=1 是布尔值,则表示查询本身或连接中存在错误。 尝试

Traceback (most recent call last):
  File "C:/Python27/triaangle.py", line 12, in <module>
    kl=l(j)
TypeError: 'list' object is not callable

找到原因。

答案 4 :(得分:0)

试试这个:

Yii::$app->request->bodyParams;

答案 5 :(得分:0)

您的代码在查询执行失败时使用mysqli_error获取更具体的错误看起来很好:

$value1 = $_POST['user']; 

$dbc = mysqli_query($conn,"SELECT * FROM userdetails 
WHERE usernamename = '$value1' "); 

if(!$dbc) {
die('DATABASE ERRORS: ' . mysqli_error($conn)); 
}

if (mysqli_num_rows($dbc)>0) {
     echo "success"; 
}
else {
     echo "duplicate entry";
}

旁注

正如我在评论中提到的,必须检查您的列名称我100%确定其错字错误,您的列名称应为username而不是usernamename