サイトの検索機能をコーディングしようとしていますが、SQLステートメントで同様の比較ができません。どういうわけか私が使うとき?そしてpindparamは、同様の比較の文字列を含む変数であり、結果が見つからずに戻ってきます。?を削除した場合 動作する比較を入力するだけpost_title LIKE '%something%'
です。
これが私のコードです:
// Retrieve search results
function retrieve_search_posts($searchfield){
//test the connection
try{
//connect to the database
$dbh = new PDO("mysql:host=localhost;dbname=mjbox","root", "usbw");
//if there is an error catch it here
} catch( PDOException $e ) {
//display the error
echo $e->getMessage();
}
$searcharray = array();
$where = "";
$searchfield = preg_split('/[\s]+/',$searchfield);
$total_words = count($searchfield);
foreach($searchfield AS $key=>$searchword){
$where .= "post_title LIKE '%".$searchword."%'";
if($key != ($total_words - 1)){
$where .= " OR ";
echo $searchword . '<br>';
}
}
echo $where;
$stmt = $dbh->prepare("SELECT p.post_id, post_year, post_desc, post_title, post_date, img_file_name, p.cat_id
FROM mjbox_posts p
JOIN mjbox_images i
ON i.post_id = p.post_id
AND i.cat_id = p.cat_id
AND i.img_is_thumb = 1
AND post_active = 1
WHERE ?
ORDER BY post_date
DESC
LIMIT 9");
$stmt->bindParam(1,$where);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$searcharray[] = $row;
}
return $searcharray;
}
// Check for errors in search field
function search_errors($searchfield){
$searcherrors = array();
if(empty($searchfield)){
$searcherrors[] = '<p>Please enter a search term.</p>';
}else if(strlen($searchfield)<3){
$searcherrors[] = '<p>Your search term must be three characters or more.</p>';
}else if(retrieve_search_posts($searchfield) == false){
$searcherrors[] = '<p>Your search for '.$searchfield.' returned no results.</p>';
}
return $searcherrors;
}
search.php
// Get the search terms posted
$searchfield = trim($_POST['searchfield']);
// Check if there are any errors with the search terms
$searcherrors = search_errors($searchfield);
// If there are errors
if(!empty($searcherrors)){
// Display them here
foreach($searcherrors AS $value){
echo $value .'<br />';
}
}
$searcharray = retrieve_search_posts($searchfield);
echo '<div id="content-wrap">';
foreach($searcharray AS $value){
$filename = substr($value['img_file_name'],9);
$cat_id = $value['cat_id'];
echo '<article class="post">';
echo '<div class="post_title">' . $value['post_title'] . '</div>';
echo '<div class="post_info">' .
'Category: ' . $cat_name = get_cat_name($cat_id) .'<br />'.
'Year: ' . $value['post_year'] .'<br />'.
$value['post_desc'] .'<br />'.
'</div>';
echo '<div class="link-to-post"><a href="#">Click to view</a></div>';
echo '<a name="'.$value['post_id'].'"></a><a href="#'.$value['post_id'].'" class="linktopost"><img class="post-thumb" src="img/thumb_/'.$filename.'" alt="MJbox Michael Jackson memorabilia thumbnail" data-postid="'.$value['post_id'].'"/></a>';
echo '<a name="'.$value['post_id'].'"></a><a href="#'.$value['post_id'].'" class="linktopost"><img class="cover-img" src="img/post-bg-1.png" alt="test" data-postid="'.$value['post_id'].'"/></a>';
echo '</article>';
}
echo '</div>';
mysqlデータベースには、where文字列に入れた単語を含むエントリが間違いなくあります。このステートメントは、.を使用する代わりに同様の比較を入力するだけで、mysqlクエリと私のWebサイトの両方で機能します?
。