warning message

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
manojsingh
Forum Newbie
Posts: 2
Joined: Mon Jan 03, 2011 12:53 pm

warning message

Post by manojsingh »

hello,
i am making a search engine but whenever i enter more then one characters in a search box it shows me this error below. Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in c:\wamp\www\major project\search.php on line 40
no result found


in the following code:

Code: Select all

<?php
//get dat
$button= $_GET['submit'];
$search= $_GET['search'];

if (!$button)
   echo "you didn't pressed the button";
else
  {
        if(strlen($search)<1)
             echo "please enter the search term first";
          else
  {
               echo "you searched for <b>$search</b><hr size='1'>";


//database connectivity
mysql_connect ("localhost","root","");
mysql_select_db ("music_park");


   //expload our search term
   $search_exploded = explode(" ", $search);
   
   foreach ($search_exploded as $search_each)
   {
     //construct query
	 
	 $x++;
	 if ($x=1)
	 $construct .= "keywords LIKE '%$search_each%'";
	 else
	 $construct .= "OR Keywords LIKE '%$search_each%'"; 
	  
	} 
	//echo out construct
	
	$construct = "SELECT * from searchengine WHERE $construct";
	$run= mysql_query($construct);
	$foundnum= mysql_num_rows($run);
	
	if ($foundnum==0)
	echo "no result found";
	else
	{
	echo "$foundnum results found<p>";
 
     }
}
}
?>
Last edited by Benjamin on Mon Jan 03, 2011 3:19 pm, edited 1 time in total.
Reason: Added [syntax=php] tags.
anantha
Forum Commoner
Posts: 59
Joined: Thu Dec 23, 2010 7:38 pm

Re: warning message

Post by anantha »

if($x=1) it should be if($x==1).

since you are using $x=1....it initializes the $x value to 1 each time..if you use $x==1..it checks whether $x is equal to 1.

Code: Select all

$construct = "SELECT * from searchengine WHERE $construct";
$run= mysql_query($construct);
$foundnum= mysql_num_rows($run);
at this point it is better to check whether the query ran correctly and then pass it to mysql_num_rows(). like

Code: Select all

$construct = "SELECT * from searchengine WHERE $construct";
$run= mysql_query($construct);
if($run)
{
$foundnum= mysql_num_rows($run);
}
else
{
.....................some code
}
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: warning message

Post by social_experiment »

If $x == 1, your query will look like :

Code: Select all

<?php "SELECT * FROM searchengine WHERE keywords LIKE '%$search_each%'"; ?>
when $x is not equal to 1, your query is going to be like this :

Code: Select all

<?php "SELECT * FROM searchengine WHERE OR keywords LIKE '%$search_each%'"; ?>
Change the innitial query to "SELECT * FROM searchengine" and modify the other queries accordingly.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply