0

I am trying to create a search, anyways my site is:

mysite.com/?p=search&t=[SEARCH VARIABLE]

So for in my case if my url is that I want it to search through my table of posts and give me the results where [SEARCH VARIABLE] exists in the table. Mainly I want it to check these columns: title, content and author. If there is any [Search variable] in there I want it to show up.

Attempt that works [with only 1 column] but does not give me all the results.

<?php
if (isset($_GET['t']))
{
    $stmt = $dbh->prepare('SELECT * FROM posts WHERE `title` like :t');
    echo ' <h1>Search results for: '.$_GET['t'].'</h1> ';
    if (!$stmt->execute(array(':t' => $_GET['t'])))
    {
        exit('Could not exec query with param: '.$_GET['t']);
    }

    while($row = $stmt->fetch(PDO::FETCH_ASSOC))
    {
        echo '<font size="4pt">'.$row["title"].'</font><br>
                        ';

    }
    }
 ?>

My attempt the same as the first one but checks the content field. Does not work either.

 <?php
    if (isset($_GET['t']))
    {
        $stmt = $dbh->prepare('SELECT * FROM posts WHERE `title` like :t AND `content` like :t;');
        echo ' <h1>Search results for: '.$_GET['t'].'</h1> ';
        if (!$stmt->execute(array(':t' => $_GET['t'])))
        {
            exit('Could not exec query with param: '.$_GET['t']);
        }

        while($row = $stmt->fetch(PDO::FETCH_ASSOC))
        {
            echo '<font size="4pt">'.$row["title"].'</font><br>
                            ';
        }
    }
  ?>

With the first one, it only shows one field with the title of it. But I want it to show anything with the word in it. For example if you search for "hey' and have "hey theres apples", i want it to show that hey theres apples.

EDIT:

<?php
if (isset($_GET['t']))
{

    $stmt = $dbh->prepare('SELECT * FROM posts WHERE `title` like %:t%');

    echo ' <h1>Search results for: '.$_GET['t'].'</h1> ';

    while($row = $stmt->fetch(PDO::FETCH_ASSOC))
    {
        echo '<font size="4pt">'.$row["title"].'</font><br>
                        ';

                            }
                            }

                    ?>

Still needing help.

2
  • @яша There is no error, its just not showing. With the first one, it only shows one field with the title of it. But I want it to show anything with the word in it. For example if you search for "hey' and have "hey theres apples", i want it to show that hey theres apples. Commented Dec 21, 2013 at 2:53
  • @JakeGould No, I dont see the answer on that. Commented Dec 21, 2013 at 3:01

1 Answer 1

0

The problem is you are not placing LIKE wildcards (%) around the search terms. Without wildcards, the query will look for the literal terms in your params which is not really what you’d want to use LIKE for.

So a statement like this:

$stmt->execute(array(':t' => $_GET['t'])

Would be this instead:

$stmt->execute(array(':t' => '"%'.$_GET['t'].'%"')

EDIT Perhaps doing it this way would be better. Leave your code as the original question, ignore the changes above and adjust your statements like this:

    $stmt = $dbh->prepare('SELECT * FROM posts WHERE `title` like %:t%');

Also for your combined statement do a similar changed and perhaps change that AND to an OR:

    $stmt = $dbh->prepare('SELECT * FROM posts WHERE `title` like %:t% OR `content` like %:t%;');

But somehow you need those wildcards in place to get the results you are implying by the question.

Sign up to request clarification or add additional context in comments.

1 Comment

Im afraid this did not work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.