Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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

share|improve this question
1  
Yeah, you're going to want to add some SQL escaping to that before you create a serious nightmare injection hole. – tadman Apr 1 '14 at 15:06
    
echo out $myquery to tell what is going wrong – Askanison4 Apr 1 '14 at 15:06
    
@Askanison4 this is displayed: Resource id #17 – user3332590 Apr 1 '14 at 15:09
    
you need to echo out the query STRING, e.g. $sql = "SELECT ...", you're echoing out the query RESULT. – Marc B Apr 1 '14 at 15:11
3  
The IN clause will look for what is on the left in the list of values on the right. For example: WHERE id IN (2,3,5) - if id is in that list it will return true. In your case it is the opposite. It is likely you will have to build the query using several of these: object_title LIKE %keyword% – Boreded Apr 1 '14 at 15:32
up vote 3 down vote accepted

It seems as though you have misunderstood how the IN clause works.

The IN clause will look for what is on the left in the list of values on the right. For example: WHERE id IN (2,3,5) - if id is in that list it will return 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.

share|improve this answer

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 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?).

share|improve this answer

Try:

$keywords_imploded = join("','", $keywords);
$myquery = sql_query("SELECT object_title FROM table WHERE object_title IN ($keywords_imploded)
share|improve this answer
    
IN('$keywords_imploded') -- if you used implode without the two inverted comma's like in OP this would break – Askanison4 Apr 1 '14 at 15:06
    
I wish i could downvote you, that is for integers only, strings need the two quotes. – user3332590 Apr 1 '14 at 15:07
    
I am all over the place... – superphonic Apr 1 '14 at 15:10
1  
It was... I got a message in my inbox. Clicked it and saw it was marked as accepted. 5 seconds later it was gone... – superphonic Apr 1 '14 at 15:53
1  
@superphonic lol thanks, at least I know I wasn't losing my mind there :p – Askanison4 Apr 1 '14 at 15:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.