简体   繁体   中英

Using PDO to make a MySQL search query and display list of results

As the title suggests, I am trying to display a list of search results using PDO and MySQL...I have a table of recipes, which have recipe_id, name & description. I would like to have a search box which can find a key word IN the name or description, ie "salad" or "carrots", and return a list of all matching recipes by displaying only their names. Before I switched to using PDO, I had the following code which did exactly what I needed:

<?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>

However, I am having difficulties doing the same with PDO...I have come up with the following code so far, but I suspect I am doing it wrong and furthermore I do not know how to display the actual results...I would be very grateful if anyone can provide some assistance, and please excuse my insufficient knowledge on the matter, I am still a newbie.

        <?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>

You need to put the % on your parameters:

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

Note, this in general is going to perform horribly as starting your like with a % will kill any index you have on name or description . As you're data grows, you'll start to see the slowdown.

Instead, you can take a look at full text searching options: http://blog.marceloaltmann.com/en-using-the-mysql-fulltext-index-search-pt-utilizando-mysql-fulltext/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM