按价格或费率排序

时间:2018-06-09 11:22:47

标签: php sql mysql-num-rows

所以这是我的代码,但每当我运行此代码时,我都会收到此消息 (注意:尝试在第31行的F:\ xampp \ htdocs \ php \ index.php中获取非对象的属性' num_rows')我无法找到问题。 我总是使用num_rows并且它有效,这是第一次发生

 <?php
$mysqli = new mysqli ('localhost','root','','instruments');
if(isset($_GET['order']))
{
$order = $_GET ['order'];
}else{
  $order = 'price';
}
if(isset($_GET['sort']))
{
$sort = $_GET ['sort'];
}else{
  $sort = 'ASC';
}

$result = $mysqli->query("SELECT * FROM bass ORDER BY $order $sort");
if($result->num_rows > 0)
{
echo "
<table border = '1'>
<tr>
<th>Price</th>
<th>Rate</th>
";
while($rows = $result->fetch_assoc())
{

      $name = $rows ['name'];
  $price = $rows ['price'];
  $rate = $rows ['rate'];
  $image = $rows ['image'];

  echo "
  <tr>
  <td>$name</td>
 <td>$price</td>
 <td>$rate</td>
 <td><img src='$image'></td>
   </tr>
  ";
}
echo "
</table>
";
}else {
  echo "Nic ni";
}
 ?>

1 个答案:

答案 0 :(得分:0)

在MySQL中运行此查询:

SELECT * FROM bass ORDER BY price ASC

检查您是否得到任何结果。也许你的查询返回0结果,这就是它失败的原因。