if condition is ???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hailmunan
Forum Newbie
Posts: 3
Joined: Wed Sep 17, 2008 4:00 pm

if condition is ???

Post by hailmunan »

hello guys..

i dont know where to start so i`ll just paste my code here ok?

Code: Select all

 
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
line 36 /* Present the search-results: */
without any results...

so how do i twist the line 31 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..
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: if condition is ???

Post by novice4eva »

rather than

Code: Select all

if ($result == true)
in line 31 use

Code: Select all

if(mysql_num_rows($result)>0)
Post Reply