PHP:警告:mysqli_num_rows()期望参数1为mysqli_result,给定字符串

时间:2016-05-08 06:48:15

标签: php mysql mysqli

我正在尝试按照以下查询获取数据

$query= "SELECT * FROM residential "; 
            if($type!=""){
             $query.=" AND  type='$type'";
            }
            if($unit_type!=""){
             $query.=" AND  unit_type='$unit_type'";
            }
            if(($min_price!="") && ($max_price!="")){
             $query.=" AND  price BETWEEN '$min_price' AND '$max_price' ";
            }
            if(($min_bedrooms!="") && ($max_bedrooms!="")){
             $query.=" AND bedrooms BETWEEN '$min_bedrooms' AND '$max_bedrooms'";
            }
            if($query==FALSE){
                echo mysqli_error($connect);
                die;
            }
            $result= mysqli_query($connect,$query);

这是我如何使用它

<div class="row">
    <?php if(mysqli_num_rows($query)>0):?>
    <?php while($row=  mysqli_fetch_assoc($query)):
        print_r($row);
        die;
    ?>

    <div class="col-md-4 col-sm-4 col-xs-12">
        <div class="row property-listing">
            <div class="col-md-6 col-sm-6 col-xs-12">
               <img src="images/1.png" class="img-responsive full-width"> 
            </div>
            <div class="col-md-6 col-sm-6 col-xs-12 property-desc">
                <h3>AED<br/>  <?php echo $row['price'];?></h3>
                <h5>Unit Type: <?php echo $row['unit_type'];?></h5>
                <h5>Available for :<?php echo $row['type'];?></h5>
                <h5>Location :<?php echo $row['area'];?></h5>
                <h5>Bedrooms :<?php echo $row['bedrooms'];?></h5>
            </div>
        </div>


    </div>

    <?php endwhile;?>
    <?php else:
        echo 'We found no record for your search criteria ';
        echo '<a href="index.html">Refine Search</a>'
        ?>

    <?php endif;?>

</div>

这就是我得到的错误

  

(!)警告:mysqli_num_rows()期望参数1为   mysqli_result,给出的字符串

发布和获取的值是正确的但查询有问题,请帮我解决一下

谢谢

4 个答案:

答案 0 :(得分:2)

$query是您的查询字符串,而不是您的结果集。所以只需使用$result

<?php if(mysqli_num_rows($result)>0):?>
    <?php while($row=  mysqli_fetch_assoc($result)):

答案 1 :(得分:1)

只需更改此代码

即可
$query= "SELECT * FROM residential "; 

进入

$query= "SELECT * FROM residential WHERE 1"; 

并更改此代码

<?php if(mysqli_num_rows($query)>0):?>
    <?php while($row=  mysqli_fetch_assoc($query)):
        print_r($row);
        die;
    ?>

<?php 
$res = mysqli_query($con, $query); //replace $con with your db connection variable
if(mysqli_num_rows($res)>0):?>
    <?php while($row=  mysqli_fetch_assoc($res)):
        print_r($row);
        die;
    ?>

答案 2 :(得分:1)

您希望mysqli_num_rows()在查询字符串中添加mysqli_fetch_assoc()mysqli_result - 在您的情况下,它是变量$result

答案 3 :(得分:0)

在您的Sql查询中,您不包含WHERE子句。

如果要将查询与第一个sql查询的其他参数连接,请将 WHERE 子句添加到第一个sql字符串中,如下所示

$query= "SELECT * FROM residential WHERE table_id!=0"; 这里 table_id 的主键。对于连接查询,这是一个很好的做法。 之后,您可以添加其他值来连接查询,如

if($type!=""){
   $query.=" AND  type='$type'";
}

所以你的最终查询看起来像是

SELECT * FROM residential WHERE table_id!=0 AND type='$type'

接下来,您将$query传递给 mysqli_num_rows ,您应该为此传递$result