eventide7 Posted February 18, 2013 Share Posted February 18, 2013 (edited) I apologize in advance for my utter noobness. I've been learning php in bits and pieces. My database structure: 3 fields only, the relevant ones being "artist" and "title" I made a search page (POETS Karaoke songs) that searches correctly, but only if the user inputs partial or complete words in the correct order, and the words only appear in one of the two fields. I wanted to make it possible to find artist/title combination when multiple or partial words are input in any order. I did something wrong, in that my new coding returns the database regardless of the search terms. This broken page is here. <form name="search" method="post" action="<?=$PHP_SELF?>"> <span> <input type="text" name="find" /> <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="Search" /> </span> </form> <? //This is only displayed if they have submitted the form if ($searching =="yes") { echo "<br><b>You searched for: " .$find; echo "<hr>"; //If they did not enter a search term we give them an error if ($find == "") { echo "<p>No search term entered. Please go back to the previous page and try again."; exit; } // Begin Search $searchQuery = ''; // search query is empty by default $searchCondition = "(artist LIKE '%%' OR title LIKE '%%')"; $searchFieldName = 'artist'; // name of the field to be searched $searchFieldName2 = 'title'; if(isset($find['text'])) { // check if a query was submitted $searchQuery = trim($find['text']); // getting rid of unnecessary white space $searchTerms = explode(" ", $searchQuery); // Split the words $searchCondition = "($searchFieldName LIKE '%" . implode("%' OR $searchFieldName LIKE '%", $searchTerms) . "%')"; // Forming the condition for the sql $searchCondition .= " OR ($searchFieldName2 LIKE '%" . implode("%' OR $searchFieldName2 LIKE '%", $searchTerms) . "%')"; } // End Search $sql = "SELECT * FROM songs WHERE $searchCondition;"; // the rest is just database connection and retrieving the results $dblink = mysql_connect("poetskaraokecom.ipagemysql.com", "poets", "guest"); mysql_select_db("poets_songs", $dblink); $result = mysql_query($sql, $dblink); echo "<ol>"; // Capture the result in an array, and loop through the array while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { printf("<li>%s<font color='grey'> ... </font><i>%s</i></li>", $row[artist], $row[title]); } echo "</ol>"; //This counts the number or results - and if there wasn't any it gives them a little message explaining that $anymatches=mysql_num_rows($result); if ($anymatches == 0) { echo "No entries matched your query<br><br>"; } } ?> I haven't been able to determine whether my error is in my query of the database, or if it's in the return of the results. Any help would be much appreciated. Thanks, Cindy Edited February 18, 2013 by eventide7 Quote Link to comment https://forums.phpfreaks.com/topic/274612-multiple-partial-search-term-query-problem/ Share on other sites More sharing options...
eventide7 Posted February 18, 2013 Author Share Posted February 18, 2013 Oh! I forgot to add that my server's MySQL version is 5.0.91 Quote Link to comment https://forums.phpfreaks.com/topic/274612-multiple-partial-search-term-query-problem/#findComment-1413052 Share on other sites More sharing options...
Barand Posted February 18, 2013 Share Posted February 18, 2013 You should look at MySQL's FULLTEXT indexing. And upgrade your PHP reference materials - they are 10 years out of date. (register_globals was off by default in version 4.2) http://php.net/manual/en/security.globals.php Quote Link to comment https://forums.phpfreaks.com/topic/274612-multiple-partial-search-term-query-problem/#findComment-1413083 Share on other sites More sharing options...
eventide7 Posted February 18, 2013 Author Share Posted February 18, 2013 Thank you. I'll do that. I've been taking bits of code from tutorials online, so I'm sure much of what I find is outdated. I'm an old dog, so I'm slower at learning new tricks, but I still like to try. Quote Link to comment https://forums.phpfreaks.com/topic/274612-multiple-partial-search-term-query-problem/#findComment-1413084 Share on other sites More sharing options...
Barand Posted February 18, 2013 Share Posted February 18, 2013 I'm an old dog, Welcome to the club Quote Link to comment https://forums.phpfreaks.com/topic/274612-multiple-partial-search-term-query-problem/#findComment-1413085 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.