使用PDO进行MySQL搜索查询并显示结果列表

时间:2014-05-09 20:57:06

标签: php mysql search pdo

正如标题所示,我正在尝试使用PDO和MySQL显示搜索结果列表...我有一个食谱表,其中包含recipe_id,name&描述。我想有一个搜索框,可以在名称或描述中找到一个关键词,即" salad"或者"胡萝卜",并通过仅显示其名称来返回所有匹配食谱的列表。在我切换到使用PDO之前,我有以下代码,它完全符合我的需要:

<?php
include ("dbconnect.php");

if (!isset($_POST['search']))    {
    header("Location:index.php");
}

$search_sql="SELECT * FROM Recipe WHERE name LIKE '%".$_POST['search']."%' OR description LIKE '%".$_POST['search']."%'";
$search_query=mysql_query($search_sql);
if (mysql_num_rows($search_query)!=0) {
    $search_rs=mysql_fetch_assoc($search_query); }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
</head>
<body>
<h3>Search results</h3>
<?php
if (mysql_num_rows($search_query)!=0) {
    do { ?>
        <p><?php echo $search_rs['name']; ?></p>
    <?php }
    while ($search_rs=mysql_fetch_assoc($search_query));
}
else {
    echo "No results found";
}
?>
</body>
</html>

然而,我在使用PDO时也遇到了困难...到目前为止,我已经提出了以下代码,但我怀疑我做错了,而且我不知道如何显示实际结果..如果有人能提供一些帮助,我将非常感激,请原谅我对此事的不充分了解,我还是新手。

        <?php
include ("dbconnect.php");

if (!isset($_POST['search']))    {
    header("Location:index.php");
}

// keep track post values
$name = "%".$_POST['search']."%";
$description = "%".$_POST['search']."%";

$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql ='SELECT * FROM recipe WHERE name LIKE ? OR description LIKE ?';
$q = $pdo->prepare($sql);
$q->execute(array($name,$description));
$data = $q->fetchAll(PDO::FETCH_ASSOC);
Database::disconnect();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <link   href="css/bootstrap.min.css" rel="stylesheet">
    <script src="js/bootstrap.min.js"></script>
    <title>Untitled Document</title>
</head>
<body>
<div class="container">
    <div class="row">
        <table class="table table-striped table-bordered">
            <thead>
                 <tr>
                    <th>Search Results</th>
                 </tr>
            </thead>
            <tbody>
    <?php
    if ($data != null) {
        foreach($data as $row)
             {
                 echo '<tr>';
                 echo '<td>'. $row['name'] . '</td>';
                 echo '</tr>';
             }
        }else
             {
                 echo '<td>'. "No results found" .'</td>';
             }?>
            </tbody>
        </table>
    </div>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

您需要在参数上加%

 // keep track post values
 $name = "%".$_POST['search']."%";
 $description = "%".$_POST['search']."%";

请注意,这通常情况会非常糟糕,因为使用%开始使用namedescription时会删除任何索引。随着数据的增长,您将开始看到减速。

相反,您可以查看全文搜索选项:http://blog.marceloaltmann.com/en-using-the-mysql-fulltext-index-search-pt-utilizando-mysql-fulltext/