[solved] Tag based search does not scan multiple tags

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

[solved] Tag based search does not scan multiple tags

Post by Sindarin »

Code: Select all

$db_select = "SELECT * FROM news WHERE news_tags='$tags' ORDER BY news.news_id ASC";
This returns the entries that their news_tags row is equal to $tags variable. However if I search for "test" and the row's value is e.g. "test testing tech", it doesn't get listed.
Last edited by Sindarin on Sat Aug 30, 2008 4:05 am, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Tag based search does not scan multiple tags

Post by Christopher »

Use LIKE and the % wildcard character:

Code: Select all

// begins with
$db_select = "SELECT * FROM news WHERE news_tags LIKE '$tags%' ORDER BY news.news_id ASC";
// ends with
$db_select = "SELECT * FROM news WHERE news_tags LIKE '%$tags' ORDER BY news.news_id ASC";
// contains
$db_select = "SELECT * FROM news WHERE news_tags LIKE '%$tags%' ORDER BY news.news_id ASC";
(#10850)
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: Tag based search does not scan multiple tags

Post by Sindarin »

It works perfectly, thanks!
Post Reply