1 Replies - 395 Views - Last Post: 13 May 2013 - 05:50 PM Rate Topic: -----

#1 rayna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-May 13

mysql_num_rows() error in search engine

Posted 13 May 2013 - 05:38 PM

Hi, I've write up my codes for search function; to include search data in the database. I have two PHP script; find.php & find_config.php.

This is my find.php page:

<form action='find_config.php' method='get'> 
    <input type='text' name='k' size='50'>
    <input type='submit' value='Search'></form>


This is my find_config.php:
<?php
   include('connection.php');
   $k =$_GET['k'];
   $terms = explode(" ", $k);
   $query = "SELECT * FROM ptreg WHERE 1=1";
  
   foreach ($terms as $each){
   		$each++;
		
		if ($each == 1)
			$query .= "keywords LIKE '%$each%' ";
			else
				$query .= "AND keywords LIKE '%$each%' ";}
    
    $run = mysql_query($query);
    $numrows = mysql_num_rows($run);
	
	if($numrows> 0){
		while($row = mysql_fetch_assoc($run)){
			$fname = $row ['fname'];
			$lname = $row ['lname'];
			$ic = $row ['ic'];
			$gender = $row ['gender'];
			$address = $row ['address'];
			$contactno = $row ['contactno'];
			$email = $row ['email'];
			
			echo "$ic<br /> $fname <br />$lname <br />";
		}
	}
	else 
		echo "No results found for \"<b>$k</b>\"";
	
	//disconnect
	mysql_close();
    ?>


However, I've encountered with this error:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in E:\xampp\htdocs\project\find_config.php on line 81
No results found for "mira"

I've tried to resolve this error by adding or die(mysql_num_rows()); at $numrows = mysql_num_rows($run) but to no avail.

Can you guys suggest codes on how I can resolve this error so that whenever I write something at my search field, it will return data from my database that I can further click on it to view it in more detail.

Is This A Good Question/Topic? 0
  • +

Replies To: mysql_num_rows() error in search engine

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5395
  • View blog
  • Posts: 27,389
  • Joined: 10-May 07

Re: mysql_num_rows() error in search engine

Posted 13 May 2013 - 05:50 PM

View Postrayna, on 13 May 2013 - 08:38 PM, said:

$run = mysql_query($query);
    $numrows = mysql_num_rows($run);



However, I've encountered with this error:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in E:\xampp\htdocs\project\find_config.php on line 81

This is one of the most common questions asked in php. You've made an assumption that your mysql query returned results. So when you run mysql_num_runs & pass in $run, you make the assumption that $run contains a value. The error reported is saying that mysql_num_rows expects the parameter to be a resource.

Validate the results.

    $run = mysql_query($query);
    if(!$run) {
      die("There was an error in the following query<hr>".$query);
    }
    $numrows = mysql_num_rows($run);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1