使用MySQL LIKE语句查找相关文章

时间:2009-08-12 11:47:10

标签: php mysql

我正在尝试获取相关文章的列表。实施例

$title = $fetch_content[article_name]; // example out put "Hello World, Ask A Question"
$rel_title = "Select * from tbl_content WHERE status='t' and article_name like '%$title' order by id desc limit 5";

如何将“Hello World,Ask A Question”分成单个关键词。

3 个答案:

答案 0 :(得分:2)

您可以使用PHP中的split函数将该标题分解为每个单词:

$title_words = split($title);

$rel_title = 'select * from tbl_content where status = \'t\' and (1=0';

foreach($title as $word)
{
    $word = str_replace(',', '', $word);
    $word = mysql_real_escape_string($word);
    $rel_title .= ' or article_title like \'%' . $word . '%\'';
}

$rel_title .= ')';

echo $rel_title;

我还使用str_replace删除了问题中的逗号。显然,如果你有其他角色,你可以将它应用于其他角色。

答案 1 :(得分:0)

应该注意的是..

like '%$title' 

...只匹配以'title'结尾的项目,因此您可能想要...

LIKE '%$title%'

...如果你只是想要包含'title'的项目。我还假设$ title变量已经被转义。如果没有,请......

LIKE '%" . mysql_real_escape_string($title) . "%'

答案 2 :(得分:0)

鲍勃,您更关注如何对文章标题中的各个关键字进行全文搜索?有些数据库支持文本搜索; MySQL的描述如下:http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html

相关问题