barkster Posted January 9, 2012 Share Posted January 9, 2012 I need to query all the fields in the table for multiple keywords. For instance I need to query for "white river" but white and river can be in any of the fields. How can I do this? I'm pretty close here but not exactly right. function queryall($table, $keyword) { $result = mysql_list_fields($this->db,$table); $sql = "SELECT * FROM ".$table." WHERE "; $keywords = explode(' ', $keyword); foreach ($keywords as $kw) { for($i = 0; $i < mysql_num_fields($result); $i++){ if($i!=0) { $kw = get_magic_quotes_gpc() ? trim($kw) : addslashes(trim($kw)); $where[] = mysql_field_name($result,$i)." LIKE '%$kw%'"; } } $whereclause[] = join(" \nOR ", $where); } $sql = $sql . join(" \nAND ", $whereclause); return $sql; } Quote Link to comment Share on other sites More sharing options...
kickstart Posted January 9, 2012 Share Posted January 9, 2012 Hi Quick play and I think something like this will so it:- <?php function queryall($table, $keyword) { $where = array(); $whereclause = array(); $result = mysql_list_fields($this->db,$table); $sql = "SELECT * FROM ".$table." WHERE "; $keywords = explode(' ', $keyword); foreach ($keywords as $kw) { $kw = get_magic_quotes_gpc() ? trim($kw) : addslashes(trim($kw)); for($i = 0; $i < mysql_num_fields($result); $i++) { if($i!=0) { $where[] = mysql_field_name($result,$i)." LIKE '%$kw%'"; } } $whereclause[] = "(".implode(" \nOR ", $where).")"; $where = array(); } $sql = $sql . implode(" \nAND ", $whereclause); return $sql; } ?> This is checking for ALL key words in any field. All the best Keith Quote Link to comment Share on other sites More sharing options...
barkster Posted January 9, 2012 Author Share Posted January 9, 2012 Hey awesome, what was I doing wrong. Looks so similar Quote Link to comment Share on other sites More sharing options...
fenway Posted January 9, 2012 Share Posted January 9, 2012 This just screams "FULLTEXT INDEX". Quote Link to comment Share on other sites More sharing options...
kickstart Posted January 9, 2012 Share Posted January 9, 2012 Hey awesome, what was I doing wrong. Looks so similar Main things were a lack of brackets and not reinitialising the $where array. All the best Keith Quote Link to comment Share on other sites More sharing options...
barkster Posted January 9, 2012 Author Share Posted January 9, 2012 Appreciate all the help, FULLTEXT INDEX does look easier... I'll look at that also Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.