OK, I have a mysql column called "description". In it is some text like: . And another one: Now, say a variable is equal to "internet" or "random", it returns the first one, else if it is equal to "rainbow" or "rainbow swims", it returns the second one. How do I match that to the description? I suppose it will involve regex . So basically you need to pull the description from the databse (which I know how to do) and if that variable has any words that are in the description, to match it to it. Now, I know this is hard for anyone, but if you do it I will, umm....service you sexually. No, I will give you rh1no.com (unused domain I bought for a friend), and rep. Thanks a lot, BP
$keywords = 'rainbow swims'; $keywords = preg_replace('~[^\w-\. ]+', ' ', trim($keywords)); $keywords = preg_quote($keywords); $keywords = array_unique(explode(' ', $keywords)); $query = sprintf("SELECT [field_names] FROM [table_name] WHERE `description` REGEXP '%s'", implode('|', $keywords)); PHP: Give this a try. (Untested)
Thank you, but before I try it I have a question: What is that? I know it has something to do with escape strings, but where does it get its value from? Thanks a helluva lot though, BP
Hmm, this doesn't seem to work with mysql_fetch_array, I get a non valid MySQL Result resource, its probably something simple. Are the tables supposed to be in the []?. //Set the count to 0 $counter = 0; //Select the right database mysql_select_db($dbname) or die (mysql_error()); $dk = $_POST['searchbox']; $dk = preg_replace('#~[^\w-\. ]+#', ' ', trim($dk)); $dk = preg_quote($dk); $dk = array_unique(explode(' ', $dk)); $keywords = sprintf("SELECT * FROM `forums` WHERE `sitedescripton` REGEXP '%s'", implode('|', $dk)); require('header.php'); //Output the data in divs. while($row = mysql_fetch_array($keywords)) PHP:
I'm not exactly sure why you modified my code, but the ~ shouldn't be in the regex line if you want to use # as delimiter. And you have to use mysql_query() on the query string before passing it to mysql_fetch_array(). $keywords = sprintf("SELECT * FROM `forums` WHERE `sitedescripton` REGEXP '%s'", implode('|', $dk)); $keywords = mysql_query($keywords) OR die(mysql_error()); PHP: My example just showed how to prepare the query string. And no, the tables shouldn't be in brackets.
I have never felt this way abot another man before I adding the #s because you didn't have the "~" at the end, and I thought ~ did something, and I lost my cheatsheet But yeah, it works (well), Thanks so much, Josh
So am I. Anyway, you want the domain? Its a crap domain, yes, but I can pint it at your nameservers should you want it.
big regexps may be too slow, why don't you do it with simple 'LIKE' queries $words = explore($query); $res = mysql_query("SELECT * FROM somewhere WHERE (description LIKE '%$query".join( "%' OR DESCRIPTION LIKE('%", $words)."%' ");