[Squashy] Posted September 21, 2010 Share Posted September 21, 2010 Ok, I have the code below and it only searches one keyword of my database please could you tell me how I can search multiple keywords? <? /* * search.php * * Script for searching a datbase populated with keywords by the * populate.php-script. */ print "<html><head><title>[squashy] Search! NOT MESSED UP.</title></head><body>\n"; if( $_POST['keyword'] ) { /* Connect to the database: */ mysql_pconnect("mysql3.000webhost.com","a4580813_dba","censored") or die("ERROR: Could not connect to database!"); mysql_select_db("a4580813_db"); /* Get timestamp before executing the query: */ $start_time = getmicrotime(); /* Execute the query that performs the actual search in the DB: */ $result = mysql_query(" SELECT p.page_url AS url, COUNT(*) AS occurrences FROM page p, word w, occurrence o WHERE p.page_id = o.page_id AND w.word_id = o.word_id AND w.word_word = \"".$_POST['keyword']."\" GROUP BY p.page_id ORDER BY occurrences DESC LIMIT ".$_POST['results'] ); /* Get timestamp when the query is finished: */ $end_time = getmicrotime(); /* Present the search-results: */ print "<h2>[squashy] Search Results For '".$_POST['keyword']."':</h2>\n"; for( $i = 1; $row = mysql_fetch_array($result); $i++ ) { print "$i. <a href='".$row['url']."'>".$row['url']."</a>\n"; print "(occurrences: ".$row['occurrences'].")<br><br>\n"; } /* Present how long it took the execute the query: */ print "This search took: ".(substr($end_time-$start_time,0,5))." seconds."; } else { /* If no keyword is defined, present the search-page instead: */ print "<form method='post'>[squashy Search] <input type='text' size='20' name='keyword'>\n"; print "Results: <select name='results'><option value='5'>5</option>\n"; print "<option value='10'>10</option><option value='15'>15</option>\n"; print "<option value='20'>20</option></select>\n"; print "<input type='submit' value='Search [squashy]'></form>\n"; } print "</body></html>\n"; /* Simple function for retrieving the currenct timestamp in microseconds: */ function getmicrotime() { list($usec, $sec) = explode(" ",microtime()); return ((float)$usec + (float)$sec); } ?> Thanks Please help Soon! Quote Link to comment https://forums.phpfreaks.com/topic/214030-please-help-search-engine-code/ Share on other sites More sharing options...
micah1701 Posted September 21, 2010 Share Posted September 21, 2010 here's what i do. i have a search box with is a form that sends the search keywords as a get variable, so when the user hits search, it goes to something like: my-site.com/search?q=These+are+my+keywords then in php i do something like: <?php $words = explode(" ",$_GET['q']); $where = "WHERE "; foreach ($words as $word){ $where.= " column_name LIKE '%".$word."%' OR"; } $where = rtrim($where,"OR");// remove that last "OR" operator $sql = "SELECT * FROM table_name $where"; $result = mysql_query($sql); // ta-da ?> Quote Link to comment https://forums.phpfreaks.com/topic/214030-please-help-search-engine-code/#findComment-1113826 Share on other sites More sharing options...
premiso Posted September 21, 2010 Share Posted September 21, 2010 Take a look at this tutorial: http://www.phpfreaks.com/tutorial/simple-sql-search Quote Link to comment https://forums.phpfreaks.com/topic/214030-please-help-search-engine-code/#findComment-1113827 Share on other sites More sharing options...
[Squashy] Posted September 21, 2010 Author Share Posted September 21, 2010 Do you need any other infomation because I do not want to follow another turoial please could you just tell me how to do it using my code? I only use that one page of code and another one to populate the database..... do you need that? But please could you just tell me how to do it using this code, I am a complete PHP n00b I no... Quote Link to comment https://forums.phpfreaks.com/topic/214030-please-help-search-engine-code/#findComment-1113831 Share on other sites More sharing options...
[Squashy] Posted September 21, 2010 Author Share Posted September 21, 2010 Please help... Quote Link to comment https://forums.phpfreaks.com/topic/214030-please-help-search-engine-code/#findComment-1113853 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.