Sikander Sikander - 7 months ago 29
SQL Question

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

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


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

Thanks

Answer

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