hailmunan Posted September 18, 2008 Share Posted September 18, 2008 hello guys.. i dont know where to start so i`ll just paste my code here ok? if( $_POST['keyword'] ) { /* Connect to the database: */ mysql_pconnect("localhost","admin","password") or die("ERROR: Could not connect to database!"); mysql_select_db("fyp"); /* Get timestamp before executing the query: */ $start_time = getmicrotime(); /* 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.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 = \"$keyword\" GROUP BY p.page_id ORDER BY occurrences DESC LIMIT $results " ); /* Get timestamp when the query is finished: */ $end_time = getmicrotime(); if ($result == true) { /* 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 "(occurrences: ".$row['occurrences'].")<br><br>\n"; } } else { print "<br> no keyword found "; } brief explanation.. actually the code is taken somewhere from the net.. its a search engine function i edited the bold section a little bit. the i added the if function.. it should work this way.. if the search function found something in the database.. it should presents the result if the search function did not found anything in the database.. (else) it should printed the " no keyword found" but.. at the (else).. it appeared the /* Present the search-results: */ without any results... so how do i twist the if ($result == true) to something that is workable? meaning.. if the the query did not found anything, it should appear the "no keyword found" thanks for the help folks.. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted September 18, 2008 Share Posted September 18, 2008 I'd check the value of mysql_num_rows($result) to see if it's not 0. Quote Link to comment Share on other sites More sharing options...
hailmunan Posted September 18, 2008 Author Share Posted September 18, 2008 wow, it works like a charm.. thanks mate.. so i did it like this.. if (mysql_num_rows($result) == 0) { print "<br> no keyword found "; } else { /* 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 "(occurrences: ".$row['occurrences'].")<br><br>\n"; } } why cant i do like this if (mysql_num_rows($result) == 1) and change the result in if end else..? it wont work.. the result is "no keyword found" for both results.. thanks.. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted September 18, 2008 Share Posted September 18, 2008 I don't understand, you CAN do mysql_num_rows($result) == 1, but it will only evaluate to true when, as you expect, the number of results is 1. Perhaps you're confusing what mysql_num_rows() does, it returns an int, not bool, the int is the number of results from the query. Quote Link to comment Share on other sites More sharing options...
hailmunan Posted September 18, 2008 Author Share Posted September 18, 2008 oh i see.. i thoought it was boolean.. how do i make it boolean? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted September 18, 2008 Share Posted September 18, 2008 You dont - you just check to make sure it's greater than or equal to one or, indeed, check to make sure it isn't 0. Afterall, we know we wont get a negative return value. Quote Link to comment Share on other sites More sharing options...
hailmunan Posted September 18, 2008 Author Share Posted September 18, 2008 understood.. i am now taking this to a new level.. i want to predefine few keywords and display it how do i do that? i have the idea of using array but it doesnt seems working for me.. heres my code //if( $_POST['keyword'] ) //{ /* Connect to the database: */ mysql_pconnect("localhost","admin","password") or die("ERROR: Could not connect to database!"); mysql_select_db("fyp"); /* Get timestamp before executing the query: */ $start_time = getmicrotime(); /* Set $keyword and $results, and use addslashes() to * minimize the risk of executing unwanted SQL commands: */ // $keyword = addslashes( $_POST['keyword'] ); $keyword = array ("malaysia", "ssd"); $results = addslashes( $_POST['results'] ); /* 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 = \"$keyword\" GROUP BY p.page_id ORDER BY occurrences DESC " ); /* Get timestamp when the query is finished: */ $end_time = getmicrotime(); if (mysql_num_rows($result) == 0) { print "<h2>Search results for '".$keyword[0]."' :</h2>\n"; print "<br> Keyword not found <br>"; } else { /* Present the search-results: */ print "<h2>Search results for ".$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 "<br> query executed in ".(substr($end_time-$start_time,0,5))." seconds."; //} //else //{ // /* If no keyword is defined, present the search page instead: */ // print "<form method='post'> Keyword: // <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'></form>\n"; //} print "</body></html>\n"; instead.. the result turn out another way.. [refer attachment] it displays as the keyword is "Array" where did i went wrong on this? and how do i display few &keyword after the result? example: $keyword[0] - found $keyword[1] - not found $keyword[2] - not found $keyword[3] - found.. help is much appreciated.. thanks [attachment deleted by admin] 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.