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.