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

新手上路,请多包涵

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

$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,给定字符串

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

谢谢

原文由 Sikander 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 806
2 个回答

只需更改此代码

$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;
    ?>

原文由 Md. Sahadat Hossain 发布,翻译遵循 CC BY-SA 4.0 许可协议

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

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

原文由 George Pant 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题