Katie Posted March 28, 2007 Share Posted March 28, 2007 Hi people, I'm trying to understand this code which has been used for a search engine, but I cant seem to get my head around. Any adivce would be brilliant result = mysql_query(" SELECT p.webpage_url AS url, COUNT(*) AS spidercounts FROM webpage p, word w, spidercount o WHERE p.webpage_id = o.webpage_id AND w.word_id = o.word_id AND w.word_word = \"$keyword\" GROUP BY p.webpage_id ORDER BY spidercounts DESC LIMIT $results" ); -------NOTE------ there are three mysql tables: - webpage, word, spidercount THANKS!!!! Link to comment https://forums.phpfreaks.com/topic/44575-help-understand-what-is-happening-in-the-code/ Share on other sites More sharing options...
per1os Posted March 28, 2007 Share Posted March 28, 2007 result = mysql_query(" SELECT p.webpage_url AS url, COUNT(*) AS spidercounts FROM webpage p, word w, spidercount o WHERE p.webpage_id = o.webpage_id AND w.word_id = o.word_id AND w.word_word = \"$keyword\" GROUP BY p.webpage_id ORDER BY spidercounts DESC LIMIT $results" ); SELECT p.webpage_url AS url, -- returns the webpage url. COUNT(*) AS spidercounts -- returns the number of spidercounts. FROM webpage p, word w, spidercount o -- From table, webpage alias p, word alias w spidercount alias o WHERE p.webpage_id = o.webpage_id -- Join the table webpage (p) and spidercount (o) on webpage_id AND w.word_id = o.word_id -- Join the table word (w) and spidercount (o) on word_id AND w.word_word = \"$keyword\" -- Make sure that the word (w) word_word is = to the keyword passed in. GROUP BY p.webpage_id -- Group them by webpage_id to avoid double webpages ORDER BY spidercounts DESC -- Order it by the number of spidercounts in descendeing order. LIMIT $results" -- Make sure it only pulls how many results is set, so if the query would return 10 but $result = 5 than only return 5 results. Link to comment https://forums.phpfreaks.com/topic/44575-help-understand-what-is-happening-in-the-code/#findComment-216733 Share on other sites More sharing options...
Katie Posted March 29, 2007 Author Share Posted March 29, 2007 Thats brilliant takes for your help Can I ask do you think this script can be adjusted so it can search for two keywords rather then one. I've tried but no luck someone told me to this ------------------------ $sql = "SELECT whatever, more, stuff FROM table WHERE "; $keywordArray = explode(" ", $_POST['keyword']); foreach ($keywordArray As $val) { $sql .= "(keyword LIKE '".$val."') "; } Then you will need to make sure the SQL is either doing an AND or OR search by replacing the )( that are rendered into ) AND ( or ) OR ( $sql = str_replace(") (", ") AND (", $sql); $sql .= "LIMIT 25;"; ------------------- how would i put that into this code ---------------------------------------- <? print "<html><head><title>My Search Engine</title></head><body>\n"; if( $_POST['keyword'] ) { include("dbconnect.php"); /* Get timestamp before executing the query: */ $start_timer = timestamp(); /* Set $keyword and $results, and use addslashes() to * minimize the risk of executing unwanted SQL commands: */ $keyword = addslashes( $_POST['keyword'] ); $results = addslashes( $_POST['results'] ); /* Execute the query that performs the actual search in the DB: */ $result = mysql_query(" SELECT p.webpage_url AS url, COUNT(*) AS spidercounts FROM webpage p, word w, spidercount o WHERE p.webpage_id = o.webpage_id AND w.word_id = o.word_id AND w.word_word = \"$keyword\" GROUP BY p.webpage_id ORDER BY spidercounts DESC LIMIT $results" ); /* Get timestamp when the query is finished: */ $stop_timer = timestamp(); $number = mysql_num_rows($result); if ($number != "0") { /* Present the search-results: */ print "<h2>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 "(spidercount: ".$row['spidercounts'].")<br><br>\n"; } } else { print "<p>Sorry no matching results found</p>"; } /* Present how long it took the execute the query: */ print "query executed in ".(substr($stop_timer-$start_timer,0,5))." seconds."; } function timestamp() { list($usec, $sec) = explode(" ",microtime()); return ((float)$usec + (float)$sec); } ?> ---------------------------------- thanks for help and time Link to comment https://forums.phpfreaks.com/topic/44575-help-understand-what-is-happening-in-the-code/#findComment-217241 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.