Author Topic: Warning: mysql_num_rows() Error  (Read 337 times)

Bryant

  • New Member
  • *
  • Posts: 7
  • Karma: 0
    • View Profile
Warning: mysql_num_rows() Error
« on: June 04, 2012, 05:40:23 PM »
Hi,

I'm very new to PHP and MySQL. But I'm pretty sure there's a simple solution to my problem, there usually is.

What I'm trying to do is create a search engine for a website but every time I enter a keyword into the search box this error comes up:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/xxxxxx/public_html/search.php on line 35
No results found for "keyword"


I underlined line 35 with a row of asterisks.

If you think you have an idea to what my problem is I would really, really would appreciate your help.

Thank you!

Here is the PHP code:

PHP Code: [Select]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head>
<
meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<
title>Search Engine</title>
</
head>
<
body>

<
center>
<
h1>Search</h1>
<
form action='search.php' method='GET'>
	
<
input type='text' name='search' size='90' value='<?php echo $_GET['search']; ?>'  /> 
	
<
input type='submit' name='submit' value='Search' >
</
center>
</
form>
<
hr />
<?
php
	
$search $_GET['search'];
	
$terms explode(" "$search);
	
$query "SELECT * FROM search WHERE ";
	

	
foreach (
$terms as $each){
	
	
$i++;
	
	
if (
$i == i)
	
	
	
$query .= "keywords LIKE '%$each%' ";
	
	
else
	
	
	
$query .= "OR keywords LIKE '%$each%' ";
	
}
	

	
// connect
	
mysql_connect("localhost""username""password");
	
mysql_select_db("databasename");

	
$query mysql_query($query);
	
$numrows mysql_num_rows($query); 
********************************************
	

	
if (
$numrows 0) {
	
	

	
	
while (
$row mysql_fetch_assoc($query)) {
	
	
	
$id $row['id'];
	
	
	
$title $row['title'];
	
	
	
$description $row['description'];
	
	
	
$keywords $row['keywords'];
	
	
	
$url $row['url'];
	
	
	

	
	
	
echo 
"<h2><a href='$url'>$title</a></h2>
	
	
	
$description<br /><br />";
	
}
	
}
	
else {
	
	
	
echo 
"No results found for \"<b>$search</b>\"";
	
	
	
//disconnect 
	
	
	
mysql_close();
	
	
 }
	
?>
</body>
</html>




RaythXC

  • PHP Programmer & Web-Designer
  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 395
  • Karma: 10
  • Freelance PHP Programmer/Web-Designer
    • View Profile
    • Rayth.Info
Re: Warning: mysql_num_rows() Error
« Reply #1 on: June 05, 2012, 09:19:47 PM »
There must be an error in your mysql syntax so the query isn't going through

Replace
$query = mysql_query($query);

with

$query = mysql_query($query) or die("Error: ".mysql_error());
RaythXC - My Home Site
Note: most answers I give come from the php manual located at PHP.Net

Bryant

  • New Member
  • *
  • Posts: 7
  • Karma: 0
    • View Profile
Re: Warning: mysql_num_rows() Error
« Reply #2 on: June 05, 2012, 11:10:47 PM »
Yes. Thank you again. I did that along with your other suggestion.

I appreciate you help.  :D