Jump to content

Warning: mysql_num_rows() Error


BryantA

Recommended Posts

Hi,

 

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

 

What I'm trying to do is create a search engine for a website that retrieves links from pages that I've placed in a table in my database according to their designated keywords. 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:

 

<!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>

 

 

Link to comment
Share on other sites

This forum has [ code ] tags that you can put around code so it displays in a readable format - please use them. You don't even have line breaks in what you posted.

 

But, from what I see the code is a mess. The problem when you see these errors is that the query failed - therefore any function that tries to use the results of the query will produce errors. You need to implement error handling into your code. Below is a rewrite of your code. I changed a lot and I'm not going to take the time to explain why I changed what I did. But, you can ask specific questions if you want.

 

<?php

//Process the input (if exists)
$searchTermsStr = isset($_POST['search']) ? trim($_POST['search']) : '';
$searchOutput = ''; //Var to hold the output

//Verify there was a valid input
if (!empty($searchTermsStr))
{
    // connect
    mysql_connect("localhost", "username", "password");
    mysql_select_db("databasename");

    //Create an array of search terms (filter out empty values)
    $searchTermsAry = array_filter(explode(" ", $searchTermsStr));

    //Process terms array into Query strings
    foreach($searchTermsAry as $key => $value)
    {
        $value = mysql_real_escape_string($value);
        $searchTermsAry[$key] = "keywords LIKE '%$value%'"
    }

    //Create and run query
    $query = "SELECT url, title, description FROM search WHERE " . implode(' OR ', $searchTermsAry);
    $result = mysql_query($query);

    //Verify query results
    if(!$result)
    {
        $searchOutput .= "There was a problem running the query:<br><br>";
        $searchOutput .= "Query: $query<br><br>";
        $searchOutput .= "Error: " . mysql_error();
    }
    elseif(!mysql_num_rows($result))
    {
        $searchOutput .= "No results found for '<b>{$searchTermsStr}</b>'";
    }
    else
    {
        while ($row = mysql_fetch_assoc($query))
        {
            $searchOutput .= "<h2><a href='{$row['url']}'>{$row['title']}</a></h2> {$row['description']}<br /><br />";
        }
    }

    //disconnect
    mysql_close();
}

?>
<!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='post'>
    <input type='text' name='search' size='90' value='<?php echo $searchTermsStr; ?>' />
    <input type='submit' name='submit' value='Search' >
    </center>
</form>
<hr />
<?php echo $searchOutput; ?>
</body>
</html>

Link to comment
Share on other sites

Psycho,

 

Thank you so much for the feedback. I thought I did use the PHP code feature for this post, I guess not  :-[. Sorry though, I'll make sure to preview my post next time.

 

But I tried to use your revised code but when I copy and paste it into Dreamweaver it shows an error on line 30 that I can't figure out. I'm sure it works. 

 

I'm pretty sure it would've been much easier to read if I would've of used the tags, silly me! But here is my original code with the code tags :D:

 

Thanks again.

 

<!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) or die(mysql_error());
$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>

Link to comment
Share on other sites

Waynewex,

 

Thank you for the advice. I added the wonderful line of code that you suggested by replacing it with that original line. And the great news is I'm no longer getting that "Warning: mysql_num_rows()..."  error message. But now it's showing me this error message:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OR keywords LIKE '%keyword%'' at line 1

 

I guess there might be something wrong with my MySQL database or table I'm not sure though. I know I have my collation set to "utf8_unicode_ci" because I heard that that was the best to use.

 

Do you have any suggestions as to why it might be saying this and what I can do to fix it?

 

Thank you so much for your help  :D

 

Side Note: I followed a YouTube video that gave instructions on how to code a search engine. That's where I got the code from. And as he was testing it when he was done it worked perfect for him. And all my original coding was exactly like his. I quadruple checked it!

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.