PHP SQL 搜索系统
我正在尝试建立一个搜索系统。我使用过mySQL系统,但我经验很少,不太了解。
- 我想通过数据库中名为 post_title 的字段进行搜索,然后获取 URL、日期,
- 我还希望能够通过标签进行搜索。为此,我们需要加入表 wp_term_relationships,它将为我们提供一个包含帖子 id 的列和一个包含标签 id 的列。由此我们可以计算出适用哪些标签。
我的尝试因致命错误而惨败
<?php
$proto = $_GET['proto'];
$terms = $_GET['f'];
if($proto == 'inline'){
$searchpattern = mysql_real_escape_string(strtoupper($terms));
$list = mysql_query("SELECT id,
post_title, post_date
FROM wp_posts
WHERE post_title LIKE '%$searchpattern%'
)
ORDER BY post_date;");
while ($row = mysql_fetch_array($list)) {
$title = $row['post_title'];
$date = $row['post_date'];
$url = $row['guid'];
$date = date($date, 'd M Y');
$return .= '<li>
<a href="#sidebar3" onClick="slide(this); return false" title="" rel="'.$id.'" style="line-height:16px;">'.$firstname.' '.$lastname.'<br /><span style="font-size:10px; color:#555;">'.$email.'</span></a>
</li>
<div class="title"><b>SEARCH RESULTS</b></div>
<a href="'.$url.'"><img src=""/>'. $title .' - <span>'. $date .'</span></a>';
}
}
?>
I am trying to build a search system. I used a mySQL system but I have little experience and I do not quite get it.
- I want to search by the field in the database called post_title and then get the URL, date
- I would also like to be able to search by tags. For that we need to join the table wp_term_relationships which will give us a column with post id's in it and a column with tag id's. From that we can then work out which tags apply.
My attempt failed miserably with FATAL ERROR
<?php
$proto = $_GET['proto'];
$terms = $_GET['f'];
if($proto == 'inline'){
$searchpattern = mysql_real_escape_string(strtoupper($terms));
$list = mysql_query("SELECT id,
post_title, post_date
FROM wp_posts
WHERE post_title LIKE '%$searchpattern%'
)
ORDER BY post_date;");
while ($row = mysql_fetch_array($list)) {
$title = $row['post_title'];
$date = $row['post_date'];
$url = $row['guid'];
$date = date($date, 'd M Y');
$return .= '<li>
<a href="#sidebar3" onClick="slide(this); return false" title="" rel="'.$id.'" style="line-height:16px;">'.$firstname.' '.$lastname.'<br /><span style="font-size:10px; color:#555;">'.$email.'</span></a>
</li>
<div class="title"><b>SEARCH RESULTS</b></div>
<a href="'.$url.'"><img src=""/>'. $title .' - <span>'. $date .'</span></a>';
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“ORDER”运算符之前有意外的“)”。
You have unexpected ")" before the "ORDER" operator.
ORDER BY 之前有一个开括号。也许这就是导致问题的原因
You have an open bracket before the ORDER BY. Maybe that's what causing the problem