<?php
include('blog/includes/config.php'); //assuming $db is a database connection, quries can be done
using $db->query()
//Post search words
$search = $_GET['search'];
// remove any code from the search term
$search = strip_tags($search);
//No keywords entered.
if ($search == "") {
echo "<p>Opps! You forgot to enter a search term.</p>";
} else {
// perform the search
$result = $db->query("SELECT * FROM blog_posts_seo WHERE MATCH(postTitle) AGAINST(':search*' IN
BOOLEAN MODE), ['search' => $search]");
echo "<h3>Search Results</h3>";
echo "<p>you searched for <b>$search</b> there are ".count($results)." matches.</p>";
foreach($results as $row) {
echo "<h1>$row->postTitle</h1>";
echo "<p>$row->desc</p>";
} // close while loop
} // close else
?>
<form action="search.php" method="get">
<input name="search" type="text" size="20" />
<input type="submit" name="submit" value="Search" />
</form>
当我在输入字段中搜索时,它显示给我这个消息:PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 SQL语法错误;查看与MySQL服务器版本对应的手册,以便在' ['search' =>پوست]'在/home/takashop/domain/takashop.ir/public_html/search.php第1行:16堆栈跟踪:#0/home/takashop/domain/takashop.ir/public_html/search.php(16): PDO->query('SELECT * FROM b…')#1 {main}抛出在/home/takashop/domain/takashop.ir/public_html/search.php第16行
我没有遵守这个。可能有一些语法错误。
//Post search words
// remove any code from the search term
$search = trim(strip_tags($_GET['search']));
//No keywords entered.
if (empty($search)) {
echo "<p>Opps! You forgot to enter a search term.</p>";
} else {
// perform the search
$stmt = $db->prepare('
Select
*
From
blog_posts_seo
Where
Match(postTitle) AGAINST(:search* IN BOOLEAN MODE);
');
$stmt->execute(['search' => $search]);
echo "<h3>Search Results</h3>";
echo "<p>you searched for <b>$search</b> there are ".$stmt->rowCount()." matches.</p>";
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) { // you don't need the PDO::FETCH_OBJ if it's in your connect statement.
echo "<h1>$row->postTitle</h1>";
echo "<p>$row->desc</p>";
} // close while loop
} // close else