<?
$hostname = 'localhost';
$username = 'mmaaaco_ksecor';
$password = 'hamilton';
$dbh = new PDO("mysql:host=$hostname;dbname=mmaaaco_ksecor", $username, $password);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<?
/*CREATE TABLE IF NOT EXISTS `3304_posts` (
  `id` int(5) NOT NULL auto_increment,
  `title` varchar(255) NOT NULL,
  `post` text NOT NULL,
  `catid` int(5) NOT NULL,
  `userid` int(5) NOT NULL,
  PRIMARY KEY  (`id`)
)  ;
CREATE TABLE IF NOT EXISTS `3304_forumcats` (
  `catid` int(5) NOT NULL auto_nicrement,
  `catname` varchar(255) NOT NULL,
  `catdesc` text NOT NULL,
  PRIMARY KEY  (`catid`)
) */

$search = trim($_POST['query']);
if ($search) {
$sql = $dbh->prepare("select 3304_posts.id,3304_posts.title,3304_forumcats.catid,3304_forumcats.catname from 3304_posts,3304_forumcats where (3304_posts.title like '%$search%' or 3304_posts.post like '%$search%' or 3304_forumcats.catname like '%$search%') and 3304_posts.catid = 3304_forumcats.catid"); //exact

$sql->execute();
$numresults = 0;
while ($row=$sql->fetch()){
$postid = $row[0];
$posttitle = $row[1];
$catid = $row[2];
$cattitle = $row[3];
$message .= 'Read Post <a href = "posts.php?id='.$postid.'">'.$posttitle.'</a><br/>click <a href = "forum.php?catid='.$catid.'">here</a> to read all posts from '.$cattitle.'<br/><br/>';
$numresults++;
}
if ($numresults == 0){
	$match = $dbh->prepare("select  id,title from 3304_posts where match(post) against ('$search')");
	$match->execute();
	while ($matchrow = $match->fetch()){
		$postid = $matchrow[0];
$posttitle = $matchrow[1];
$message .= 'Read Post <a href = "posts.php?id='.$postid.'">'.$posttitle.'</a><br/><br/>';
	$numresults++;	
	}
	
	
}
echo 'Your search for <strong>\''.$search.'\'</strong> generated ('.$numresults.') results<br/><br/>';  
echo $message;
}
?>

<form action = "searchanswer.php" method = "post">
  Search:
  <input type = "text" name = "query" />
  <br/>
  <input type = "submit" name = "submit" value = "Search!" />
</form>
</body>
</html>
