PHP :Warning: mysqli_num_rows() 期望参数 1 是 mysqli_result,给出的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37097062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:03:37  来源:igfitidea点击:

PHP :Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, string given

phpmysqlmysqli

提问by Sikander

i am trying to fetch data as per following query

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

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

this is how i use it

这就是我使用它的方式

<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>

This is what i get as error

这是我得到的错误

( ! ) Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, string given

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

values being posted and fetched are correct but something is wrong with query , Please help me sort it out

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

Thanks

谢谢

回答by George Pant

$queryis your query string not your result set.So just use $result

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

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

回答by Md. Sahadat Hossain

Just change this code

只需更改此代码

$query= "SELECT * FROM residential "; 

Into

进入

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

And change this code

并更改此代码

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

To

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

回答by Yair.R

You are adding to mysqli_num_rows()and mysqli_fetch_assoc()the query string when it expects mysqli_result- In your case its the variable $result.

要添加到mysqli_num_rows(),并mysqli_fetch_assoc()在预计的查询字符串mysqli_result-在你的情况下,它的变量$result

回答by Mujahidh

In your Sql query you do not include a WHERE clause.

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

If you want to concatenate your query with other argument for the first sql query add a WHEREclause to your first sql string like this

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

$query= "SELECT * FROM residential WHERE table_id!=0";Here table_idis the primary key of your table.it will be a good practice for concatenating the query. after that you can add another values to concatenate the query like

$query= "SELECT * FROM residential WHERE table_id!=0";这里table_id是你的的主键。这将是连接查询的好习惯。之后,您可以添加另一个值来连接查询,如

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

so your final query something looks like

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

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

Next here you are passing $queryto mysqli_num_rowsyou should pass the $resultfor this

接下来,您将传递$querymysqli_num_rows,您应该$result为此传递