Page 1 of 1

LIKE query

PostPosted: November 29th, 2011, 8:17 am
by epixmedia
Hi all, I'm using the following query to perform a search on the member database, specifically the custom s2member fields.


$users = $wpdb->get_results ("SELECT `user_id` as `ID` FROM `" . $wpdb->usermeta . "` WHERE `meta_key` = '" . $wpdb->prefix . "s2member_custom_fields' AND `meta_value` REGEXP '.*\"".$searchfield."\";s:[0-9]+:\"".$searchvar."\".*'");


How do I alter this so it looks for matches LIKE the $searchvar instead of exact matches? for example, in regular SQL it would be, ...where $searchfield LIKE '%$searchvar%' so "epix" would match "epix media" or "somethnig epix something" for example...

Thank you :)

Zoe

Re: LIKE query

PostPosted: December 1st, 2011, 3:15 am
by epixmedia
Is anyone able to help me on this? I need to be able to search for non-exact results...

Cheers,

Zoe

Re: LIKE query

PostPosted: December 2nd, 2011, 3:11 am
by Cristián Lávaque
These references may help:

http://dev.mysql.com/doc/refman/5.0/en/regexp.html
http://dev.mysql.com/doc/refman/5.0/en/ ... ching.html

I haven't used regex in queries yet, so I had to look them up, I'm familiar with PHP ones.

I haven't tested this, but you could try .* before and after the $searchvar:

'.*\"" . $searchfield . "\";s:[0-9]+:\".*" . $searchvar . ".*\".*'

I hope it helps. :)

Re: LIKE query

PostPosted: December 2nd, 2011, 4:04 am
by epixmedia
Cheers Cristian, I'll give it a try and let you know the results!!

Re: LIKE query

PostPosted: December 2nd, 2011, 4:11 am
by epixmedia
hmm, no luck yet. Jason helped me with the original code to get the search to work - if he's about could you see if perhaps he has time to look at this thread please?

In the meantime I will continue to Google!

Thanks,

Zoe

Re: LIKE query

PostPosted: December 2nd, 2011, 4:19 am
by epixmedia
Scratch that! It does work - it was a different part of my code that's not working! Thanks Cristian - much appreciated!

Re: LIKE query

PostPosted: December 4th, 2011, 3:27 am
by Cristián Lávaque
Excellent! :)