简体   繁体   English

将数组内嵌到MySQL查询中

[英]Imploding an array into a MySQL query

so I have a blog system, and i want to build a section for "related news", I am not making a 'Tags' system but just simply searching and storing the current title of the article (which is pulled from the database) in a string and exploding it to be able to later put all the words into a query, that later query will search all titles in the database to find any of those words, and if it does, it will return the title in a list. 所以我有一个博客系统,并且我想为“相关新闻”构建一个部分,我不是在建立“标签”系统,而只是在其中搜索和存储文章的当前标题(从数据库中提取)。一个字符串并将其展开,以便以后可以将所有单词放入查询中,该稍后查询将在数据库中搜索所有标题以找到这些单词中的任何一个,如果匹配,它将在列表中返回标题。 Here is the relevant code: 以下是相关代码:

// note to stackoverflow peeps, $row_object_title is just the title that is pulled form the database
$row_object_title_lower = strtolower($row_object_title);
$keywords = explode(" ",$row_object_title_lower);

Code that is run later on the page: 稍后在页面上运行的代码:

$keywords_imploded = implode("','",$keywords);
$myquery = sql_query("SELECT object_title FROM table WHERE object_title IN ('$keywords_imploded')

Now i try to list the titles by printing the title out, but nothing is display. 现在,我尝试通过打印出标题来列出标题,但是什么也没有显示。

I am sure there is matching titles in the database. 我确定数据库中有匹配的标题。

Thanks 谢谢

Your array of keywords is generated with: 您的关键字数组是通过以下方式生成的:

$keywords = explode(" ",$row_object_title_lower);

What if you have a title like "My Super Blog Post"? 如果您拥有“我的超级博客文章”这样的标题怎么办? You're going to get: 您将获得:

$keywords = array( "My", "Super", "Blog", "Post" );

Later on, you query using those values imploded together: 稍后,您使用内插在一起的值查询:

$keywords_imploded = implode("','",$keywords);
$myquery = sql_query("SELECT object_title FROM table WHERE object_title IN ('$keywords_imploded')

The SELECT query is going to look like this: SELECT查询将如下所示:

SELECT object_title FROM table WHERE object_title IN ( 'My', 'Super', 'Blog', 'Post' );

I don't think that's going to find anything. 我认为不会找到任何东西。

You need to re-evaluate how you're handling the list of titles (I think that's what you're going for, right?). 您需要重新评估标题列表的处理方式(我想这就是您想要的,对吧?)。

It seems as though you have misunderstood how the IN clause works. 好像您误解了IN子句的工作方式。

The IN clause will look for what is on the left in the list of values on the right. IN子句将在右侧的值列表中查找左侧的内容。 For example: WHERE id IN (2,3,5) - if id is in that list it will return true. 例如:WHERE id IN(2,3,5)-如果id在该列表中,它将返回true。 In your case it is the opposite. 在您的情况下,情况恰恰相反。

Something like this should work for what your after but there are likely to be better alternatives. 这样的事情应该可以满足您的需求,但是可能会有更好的选择。

$sql = '';
foreach ($keywords AS $keyword)
{
    if ($sql != '')
        $sql .= ' OR ';

    $sql .= "object_title LIKE '%$keyword%'";
}

$query = 'SELECT object_title FROM table WHERE '.$sql;

% is a wildcard. %是通配符。

Just please remember to escape the values first. 请记住,请先逃避这些值。

Try: 尝试:

$keywords_imploded = join("','", $keywords);
$myquery = sql_query("SELECT object_title FROM table WHERE object_title IN ($keywords_imploded)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM