PDA

View Full Version : mysql_num_rows() error


sydesign
02-27-2012, 09:56 PM
I am trying to create a search engine of sorts on a site for my client. the php code is

<?php
$k = $_GET ['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM search WHERE ";

foreach ($terms as $each){
$i++;

if ($i == 1)
$query .= "keywords LIKE '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";

}

// connect
mysql_connect("localhost", "root", "root");
mysql_select_db("searchengine");

$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0){

while ($rows = mysql_fetch_assoc($query)){
$title = $rows['title'];
$description = $rows['description'];
$keywords = $rows['keywords'];
$link = $rows['link'];

echo "<h2><a href='$link'>$title</a></h2>
$description<br /><br />";
}

}
else
echo "No results found for \"<b>$k</b>\"";

//disconnect
mysql_close();
?>

when i submit this form:

<form method="get" action="results.php">
<input type="text" name="k" size="30" value="<?php echo $_GET['k']; ?>"/>
<input type="submit" value="Search" />
</form>


i get this error:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in ***.php on line 228

I've been searching around for why this happens and i haven't found a reason that helps me. This is my first time teaching myself and trying to learn phpmyadmin and mysql databases so i don't know if maybe I'm doing something wrong...

thank you so much for any help you can offer!

gentleone
02-27-2012, 10:10 PM
This has nothing to do with Spry. Moved to the PHP board.

sydesign
02-27-2012, 10:14 PM
This has nothing to do with Spry. Moved to the PHP board.
ah yes sorry I think I was distracted ... thank you.

edbr
02-28-2012, 12:29 AM
is this the line if ($numrows > 0?

just before it, add line echo $numrows: see what is returning

sydesign
02-28-2012, 04:42 AM
like this?

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

echo $numrows;

if ($numrows > 0){



i still get the same result and error..

which is kind of interesting now because it still says on line 228 but theres nothing on line 228 anymore

edbr
02-28-2012, 06:31 AM
yes but its not a cure, its to echo out what is being returned as $numrows

sydesign
02-28-2012, 05:35 PM
yea i know. i just meant that i didn't see anything different come up. i didn't see it echo any results. does this mean theres a problem with the code? or the database?

Im not sure..

edbr
02-29-2012, 01:30 AM
i would guess code as its not returning row count

edbr
02-29-2012, 01:35 AM
for one put ypour connect and select database before the select query