在php No Search错误消息中搜索按钮

时间:2015-09-17 13:12:49

标签: php mysqli mysql-num-rows

我遇到麻烦我应该把我的echo 'No Result Found';放在代码中我尝试将它放在最后一个else语句中一起用于调试代码但是它不起作用。请帮助我在哪里找到我的无结果。提前谢谢。

if(!empty($_POST['search'])){
if($result = $db->query("SELECT * FROM product WHERE setname like '%".$_POST['search']."%' OR category like '%".$_POST['search']."%' ")) {

while($row = $result->fetch_assoc())    {
echo '<div class="col-sm-3">';
    echo '<form method="POST" action="buynow.php" enctype="multipart/form-data"> ';
        echo '<input type="hidden" name="productid" value="'.$row['id'].'">';
        echo '<img class="thumbnail img-responsive" src="data:image;base64,'.$row['image'].'  " >'; 
        echo '<p>Name :  ',$row['setname'], '</span></p>';
        echo '<p>Price :  ', $row['price'], '</span></p>';
        echo '<p>Bonus :  <span class="label label-info" style="font-size:16px;">', $row['status'], '</span></p>';
        echo '<p>Price Now :  ', $row['pricesale'], '</span></p>';
        echo '<p>Product Detail: ', $row['productdesc'] ,'</p>';
        echo '<button class="btn btn-danger btn-lg btn-block" type="submit" name="submit">Buy Now</button>';
        echo '</form>'; 
    echo '<br></div>';
}
echo '</div>';
echo '</div>';

}else{
//code for debugging query      
die($db->error);
}
}

3 个答案:

答案 0 :(得分:2)

您需要从查询中计算num_rows的数量。如果没有找到行显示没有找到结果

  if (!empty($_POST['search'])) {
        if ($result = $db->query("SELECT * FROM product WHERE setname like '%" . $_POST['search'] . "%' OR category like '%" . $_POST['search'] . "%' ")) {
            $row = $result->num_rows;
            if ($row > 0) {
                while ($row = $result->fetch_assoc()) {
                    echo '<div class="col-sm-3">';
                    echo '<form method="POST" action="buynow.php" enctype="multipart/form-data"> ';
                    echo '<input type="hidden" name="productid" value="' . $row['id'] . '">';
                    echo '<img class="thumbnail img-responsive" src="data:image;base64,' . $row['image'] . '  " >';
                    echo '<p>Name :  ', $row['setname'], '</span></p>';
                    echo '<p>Price :  ', $row['price'], '</span></p>';
                    echo '<p>Bonus :  <span class="label label-info" style="font-size:16px;">', $row['status'], '</span></p>';
                    echo '<p>Price Now :  ', $row['pricesale'], '</span></p>';
                    echo '<p>Product Detail: ', $row['productdesc'], '</p>';
                    echo '<button class="btn btn-danger btn-lg btn-block" type="submit" name="submit">Buy Now</button>';
                    echo '</form>';
                    echo '<br></div>';
                }
            } else {
                echo "No result Forund";
            }
        } else {
//code for debugging query      
            die($db->error);
        }
    }
    echo '</div>';
    echo '</div>';

答案 1 :(得分:0)

您可以使用$result->num_rowsmysqli_num_rows($result)

if ($result->num_rows == 0) {
    echo 'No Result Found';
}
else {
    // Do your while
}

请使用prepared statements

答案 2 :(得分:0)

  1. 使用准备好的陈述。
  2. 使用PDO,而不是mysqli。
  3. 使用模板。
  4. 所以它是

    $sql = "SELECT * FROM product WHERE setname like :search OR category like :search";
    $stmt = $pdo->prepare($sql);
    $stmt->execute(array('search' => '%'.$_POST['search'].'%'));
    $data = $stmt->fetchAll();
    ?>
    <? foreach($data as $row): ?>
    <div class="col-sm-3">
        <form method="POST" action="buynow.php" enctype="multipart/form-data">
            <input type="hidden" name="productid" value="<?=$row['id']?>">
            <img class="thumbnail img-responsive" src="data:image;base64,<=$row['image']?>"> 
            <p>Name :  <?=$row['setname']?></span></p>
            <p>Price :  <?=$row['price']?></span></p>
            <p>Bonus :  <span class="label label-info" style="font-size:16px;">
                <?=$row['status']?>
            </span></p>
            <p>Price Now :  <?=$row['pricesale']?></span></p>
            <p>Product Detail: <?=$row['productdesc']?></p>
            <button class="btn btn-danger btn-lg btn-block" type="submit" name="submit">Buy Now</button>
        </form>
        <br>
    </div>
    <? endforeach ?>
    <? if (!$data): ?>
        No Result Found
    <? endif ?>