PHP SQL 搜索系统

发布于 2024-12-19 14:04:34 字数 1265 浏览 0 评论 0原文

我正在尝试建立一个搜索系统。我使用过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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只有一腔孤勇 2024-12-26 14:04:34
 $list = mysql_query("SELECT id,  post_title,  post_date 
FROM wp_posts 
WHERE post_title LIKE '%$searchpattern%') ORDER BY post_date;");

“ORDER”运算符之前有意外的“)”。

 $list = mysql_query("SELECT id,  post_title,  post_date 
FROM wp_posts 
WHERE post_title LIKE '%$searchpattern%') ORDER BY post_date;");

You have unexpected ")" before the "ORDER" operator.

无声静候 2024-12-26 14:04:34

ORDER BY 之前有一个开括号。也许这就是导致问题的原因

You have an open bracket before the ORDER BY. Maybe that's what causing the problem

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文