教程改进或过滤php搜索

时间:2014-02-11 22:29:34

标签: php mysql search filter

有没有人知道如何在搜索完成后如何优化过滤php / mysql搜索的教程?我搜索过谷歌和每个可能的搜索引擎,但没有。

提前谢谢

<?php
if(isset ($_POST['searchString'])){

    $searchStr = $_POST['searchString'];

    function sanitizeString($searchStr){
        $searchStr = htmlentities($searchStr);
        $searchStr = strip_tags($searchStr);
        $searchStr = htmlspecialchars($searchStr);
        return $searchStr;
    }

    $searchStr = sanitizeString($searchStr);
    $searchStr = str_replace("%", "", $searchStr);
    require_once('Connections/conn.php');


$sql = "SELECT * FROM happyhours
WHERE city LIKE :keyword OR zipcode LIKE :keyword";



    if (!empty($searchStr)){

        $stmt=$conn->prepare($sql);
        $stmt->bindParam(':keyword',$searchStr, PDO::PARAM_STR);

            try{
                $stmt->execute();
                }
            catch(PDOException $ex){
                echo $ex->getMessage();
                }
    }
        $count = $stmt->rowCount();
        $each = $stmt->fetch(PDO::FETCH_ASSOC);
}

?>

以下输出结果

<?php do { ?>

<table id="results" align="left" width="700px" border="0">
      <tr>
        <td rowspan="5" width="130" id="photo"><a class="lightbox" href='<?php echo $each['imageURL']; ?>'><p><?php echo $each['name']; ?></p><img id="hh-image" src='<?php echo $each['imageURL']; ?>' width="80" height="80" /></a></td>

        <tr><td id="hh-name" style="word-wrap:break-word; font-size:16px; font-family:'Myriad Pro'; font-weight:500" width="560" height="20"><?php echo $each['name']; ?></td></tr>

         <tr><td> <a href='<?php echo $each['googleMap']; ?>' target="new" style="font-size:14px"><?php echo $each['address']; ?></a></td>
      </tr>
      <tr>
        <td style="word-wrap:break-word; font-size:14px; font-family:'Myriad Pro'" height="20"><?php echo $each['phone']; ?></td>
      </tr>
      <tr>
        <td style="word-wrap:break-word; font-size:14px; font-family:'Myriad Pro'" height="20"><?php echo $each['dayOfTheWeek']; ?>&nbsp;&nbsp;(<?php echo $each['hours']; ?>)</td>
      </tr>
    </table>
    <br/>


<?php } while($each = $stmt->fetch(PDO::FETCH_ASSOC)); ?>



<?php  }  ?>

1 个答案:

答案 0 :(得分:0)

This blog有一个部分提供了有关过滤MYSQLi返回的简要概述。有关详细信息,建议您查看MYSQLiPDO文档。

(当然,我真正推荐的是检查PHP的许多框架之一! - 我看到tadman已经在评论中提供了一个很好的列表链接。)