Page 1 of 1

Valid regex (old?) code in a query??

Posted: Wed Mar 22, 2006 5:06 pm
by rubberjohn
I'm trying to make a simple search function that takes a string of user input and returns matches based on the number of keywords that match.

I am basing this on code that is about 5 years old - is it still valid as I am having trouble getting the REGEX to work? I am using PHP Version 4.4.2.

Code: Select all

function search($query_terms){

$tags = explode(' ', addslashes(strtolower($tags)));
$reg_ex = "[(" . implode(")(", $tags) . ")]";

$sql_get_tags =mysql_query( "SELECT count(f_tags.tag_id) as score,

$tag_link.user_id as f_user_id
FROM f_tags, $tag_link
WHERE ($tag_link.tag_id = f_tags.tag_id)
AND (f_tags.tag REGEXP $reg_ex)") or die (mysql_error());

   
while($get_tags = mysql_fetch_array($sql_get_tags)) {
print_r($get_tags);
} 

   }


Thanks in advance

rj

Posted: Wed Mar 22, 2006 5:17 pm
by feyd
It's not valid for MySQL's REGEXP, although it appears $tags magically appears. :?

You may want to test if LIKE or FULL TEXT works better for this.

Posted: Wed Mar 22, 2006 9:09 pm
by Benjamin
Maybe it's out of a class. You may need to add $tags to the function parameters if it isn't anymore.

Posted: Thu Mar 23, 2006 6:56 am
by rubberjohn
so if a LIKE statement was used instead:

Code: Select all

SELECT "SOMETHING"
FROM "TABLE"
WHERE "SOMETHING" LIKE {PATTERN}
I know PATTERN can be a variable for finding a single item. If you had multiple search items which could vary in number, would it be best to use an array in a for_each?

thanks

rj

Posted: Thu Mar 23, 2006 10:14 am
by feyd

Code: Select all

SELECT `field`
FROM `table`
WHERE `field` LIKE '%{$term1}%'
OR `field` LIKE '%{$term2}%'
etc etc
However, it could slow down the query. You should probably do several tests to find out which works best for what you need.

Posted: Thu Mar 23, 2006 10:28 am
by rubberjohn
i think it probably would because the number of keywords submitted could be a lot