jakebur01 Posted October 10, 2007 Share Posted October 10, 2007 I have a web site with several thousand items, all the items are stored in a mysql database. I am trying to have it to where the user can enter a phrase and the search would pull the rows that relate. Problem: The way that I have it written now, users can only enter searches containing one or two words. And it works great with one or two words. But, I would like to update it to where if the client enters Ex. "Sx-495 Belt Starter" that it will still pull rows from the database with those words. Here's the way it is set up now. And they way I have these "or's" in the select statement, is this meaning one row or the other? function get_search_books($mysearch) { // query database for the books in a category $mysearch =$_POST['searchlike']; $mysearch= trim($mysearch); if (!$mysearch || $mysearch=='') return false; $conn = db_connect(); $sql = "SELECT No, description, title, author, isbn, price FROM books "; $sql .= "WHERE `No` LIKE '%$mysearch%' "; $sql .= "OR `description` LIKE '%$mysearch%' "; $sql .= "OR `title` LIKE '%$mysearch%' "; $sql .= "OR `author` LIKE '%$mysearch%' "; $sql .= "OR `isbn` LIKE '%$mysearch%' "; $result = @$conn->query($sql); if (!$result) return false; $num_books = @$result->num_rows; if ($num_books ==0) return false; $result = db_result_to_array($result); return $result; } Thanks, Jake Quote Link to comment https://forums.phpfreaks.com/topic/72608-solved-searching-phrases/ Share on other sites More sharing options...
d22552000 Posted October 10, 2007 Share Posted October 10, 2007 this SHOULD work for multiple words , althogh it would have to consists of all the words mentinoed for it to work, how about your split the search terms by spaces like if they entere three words "this my search" then you would have $s[0]='this' $s[1]='my' $s[2]='search' use split(" ",$mysearch) for this. Now that you have the search terms split, loop throgh them: foreach($s as $mysearch) { } FULL CODE BELOW: function get_search_books($mysearch) { // query database for the books in a category $mysearch =$_POST['searchlike']; $mysearch= trim($mysearch); if (!$mysearch || $mysearch=='') return false; $conn = db_connect(); /* START OF EDIT */ $MAXWORDS = 5; /* It has to query PER word, so set the max search terms here. It will treat the last search term as all remaining words that have not been searched for, so if this is 3 then the third search to the db is everything NOT in the second and first searches. */ $s = split(' ', $mysearch, $MAXWORDS); $result = ''; /* This sets result as a blank string , to aviod the NOTICE of appening data to nothing */ foreach($s as $mysearch) { $sql = "SELECT No, description, title, author, isbn, price FROM books "; $sql .= "WHERE `No` LIKE '%$mysearch%' "; $sql .= "OR `description` LIKE '%$mysearch%' "; $sql .= "OR `title` LIKE '%$mysearch%' "; $sql .= "OR `author` LIKE '%$mysearch%' "; $sql .= "OR `isbn` LIKE '%$mysearch%' "; $result.= @$conn->query($sql); /* Notice the . before the = , this appends the results to the previous results. */ } /* END OF EDIT */ if (!$result) return false; $num_books = @$result->num_rows; if ($num_books ==0) return false; $result = db_result_to_array($result); return $result; } Quote Link to comment https://forums.phpfreaks.com/topic/72608-solved-searching-phrases/#findComment-366107 Share on other sites More sharing options...
jakebur01 Posted October 10, 2007 Author Share Posted October 10, 2007 PHP Catchable fatal error: Object of class mysqli_result could not be converted to string in book_fns.php on line 183 Quote Link to comment https://forums.phpfreaks.com/topic/72608-solved-searching-phrases/#findComment-366109 Share on other sites More sharing options...
d22552000 Posted October 10, 2007 Share Posted October 10, 2007 coudl you please open yoru script in notpad and use "edit => goto" and please point out what is line 183 Quote Link to comment https://forums.phpfreaks.com/topic/72608-solved-searching-phrases/#findComment-366123 Share on other sites More sharing options...
jakebur01 Posted October 10, 2007 Author Share Posted October 10, 2007 I'm sorry. Here is line 183. $result.= @$conn->query($sql); /* Notice the . before the = , this appends the results to the previous results. */ Quote Link to comment https://forums.phpfreaks.com/topic/72608-solved-searching-phrases/#findComment-366128 Share on other sites More sharing options...
jakebur01 Posted October 10, 2007 Author Share Posted October 10, 2007 Why will it not convert to a string? Quote Link to comment https://forums.phpfreaks.com/topic/72608-solved-searching-phrases/#findComment-366140 Share on other sites More sharing options...
d22552000 Posted October 10, 2007 Share Posted October 10, 2007 no clue, beacues I can do $row = $results; and ti works fine, I dont know why your connection is a class, but I guess, whatever suites you. Quote Link to comment https://forums.phpfreaks.com/topic/72608-solved-searching-phrases/#findComment-366142 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.