亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果實時搜索框輸入與數據庫不匹配,如何顯示未找到數據?

如果實時搜索框輸入與數據庫不匹配,如何顯示未找到數據?

PHP
開滿天機 2021-04-30 14:15:57
<?php$output = "";if(isset($_POST["query"])){ $search = $_POST["query"]; $sql = ("  SELECT * FROM `products`   WHERE productName LIKE :sText  OR productLine LIKE :sText  OR productVendor LIKE :sText  OR MSRP LIKE :sText "); $stmt=$db->prepare($sql);}elseif{ $sql = "SELECT * FROM products"; $stmt=$db->prepare($sql);}$stmt->execute(array(':sText'=>'%'. $search .'%'));foreach($stmt as $row){ $output = "           <div class='product_wrapper'>        <form method='post' action=''>        <table style='width:170px;height:143px;''>        <tr>        <td><input type='hidden' name='productCode' value=". $row['productCode']." />        <div class='name'>".$row['productName']."</div>        <div class='price'>RM". $row['MSRP']."</div></td>        </tr>        <table style='width:170px;height:60px'>        <tr>        <td><button type='submit' class='buy' style='margin: : 25px;'>Buy Now</button></td>        </tr>        </table>        </table>        </form>          </div> "; echo $output;}else{ echo 'Data Not Found';}?>為了添加其他未找到的回顯數據,我對elseif條件所需的條件有疑問。我必須找出可以添加的elseif條件。感謝您的幫助。
查看完整描述

2 回答

?
POPMUISE

TA貢獻1765條經驗 獲得超5個贊

您不能將else與foreach一起使用,因此需要在使用foreach語句之前檢查是否有任何結果。


if($stmt->rowCount() > 0) {

    foreach($stmt as $row)

    {

        $output = echo" 

          <div class='product_wrapper'>

        <form method='post' action=''>

        <table style='width:170px;height:143px;''>

        <tr>

        <td><input type='hidden' name='productCode' value=". $row['productCode']." />

        <div class='name'>".$row['productName']."</div>

        <div class='price'>RM". $row['MSRP']."</div></td>

        </tr>

        <table style='width:170px;height:60px'>

        <tr>

        <td><button type='submit' class='buy' style='margin: : 25px;'>Buy Now</button></td>

        </tr>

        </table>

        </table>

        </form>

        </div>";


     $output;

    }

} else {

    echo 'Data Not Found';

}


查看完整回答
反對 回復 2021-05-14
?
梵蒂岡之花

TA貢獻1900條經驗 獲得超5個贊

您可以rowCount用來獲取受影響的行數


試試這個


 if($stmt->rowCount() > 0) {

   //Loop throught the rows

 }else{

    echo "No matching records found.";

 }

檢查更多信息PDOStatement :: rowCount


查看完整回答
反對 回復 2021-05-14
  • 2 回答
  • 0 關注
  • 163 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號