什么是获取相关内容的好方法

时间:2012-07-02 12:55:20

标签: php mysql

在php中获取相关内容有什么好方法。我有$关键字,如果我在其中使用标题LIKE'%$ keyword%'它返回same.my方法很简单:

$keywords = 'Most Perfectly Timed Videos EVER';
$rel = mysql_query("select * from media where title LIKE '%$keywords%'")or
die(mysql_error());

我希望最大结果与我的关键字相关。 感谢。

1 个答案:

答案 0 :(得分:1)

$query = "SELECT * FROM media WHERE ";
$keywords = 'Most Perfectly Timed Videos EVER';
$keywords = explode(" ", $keywords);
$where = "";
foreach($keywords as $k => $v)
{
    $where .= "title LIKE '%".$v."%' AND ";
}
$where = substr($where, 0, -5); //deleting the last "AND"

$query .= $where;
$rel = mysql_query($query) or die(mysql_error());

或者,如果你想使用“OR”而不是“AND”来获得更好的结果:

foreach($keywords as $k => $v)
{
    $where .= "title LIKE '%".$v."%' OR ";
}
$where = substr($where, 0, -4);