-6

HTML code for the search engine which I believe is correct Search

My Search Engine

PHP code starts here where I believe the error is coming from

if(!$button)
echo "you didn't submit a keyword"; else
{
if(strlen($search)<=1)
echo "Search term too short";
else{
echo "You searched for <b>$search</b> <hr size='1'></br>";
mysql_connect("localhost","root","");
mysql_select_db("testproject");

$search_exploded = explode (" ", $search);
foreach($search_exploded as $search_each)
{
$x = NULL; $construct = NULL;
$x++;
if($x==1)
$construct .="keywords LIKE '%$search_each%'";
else
$construct .="AND keywords LIKE '%$search_each%'";
}
$construct ="SELECT * FROM members WHERE $construct";
$run = mysql_query($construct);

$foundnum = mysql_num_rows($run);
if ($foundnum==0)
echo "Sorry, there are no matching result for <b>$search</b>.
</br></br>1. Try more general words.";
else
{
echo "$foundnum results found !<p>";

while($runrows = mysql_fetch_assoc($run))
{
$username = $runrows ['username'];
$email = $runrows ['email'];

} 
}
}
}
?>

keep getting: Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in

3 Answers3

1

I think this is just a space problem in your query, just try to add spaces like

$construct .=" AND keywords LIKE '%$search_each%'";

Also note that keyword should be the name of table's column where you want to look for.

I would like to also to remember you that mysql_ functions are deprecated so i would advise you to switch to mysqli or PDO and indeed you are at risk of sql injection

Fabio
  • 23,183
  • 12
  • 55
  • 64
  • This is what comes back: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 'AND keywords LIKE '% – user2305699 Apr 28 '13 at 20:36
  • Try to print your query to see how it looks like. I would suggest to change your query in `$construct_query = "SELECT * FROM members WHERE $construct"; echo $construct_query; $run = mysql_query($construct_query);` – Fabio Apr 28 '13 at 20:38
0

That's probably because the SQL request failed. Try to append or die(mysql_error()) next to $run = mysql_query($construct), like this :

$run = mysql_query($construct) or die(mysql_error());

This should output the error so that you can fix it.

Maxime
  • 388
  • 3
  • 10
0

Probably, there is an error in the query. Try this:

$run = mysql_query($construct);
if (!$run)
  echo 'an error occurred: '.mysql_error();
else {
  $foundnum = mysql_num_rows($run);
  if ($foundnum==0)
  echo "Sorry, there are no matching result for <b>$search</b>.
  </br></br>1. Try more general words.";
else {
  echo "$foundnum results found !<p>"; 
  while($runrows = mysql_fetch_assoc($run)) {
    $username = $runrows ['username'];
    $email = $runrows ['email']; 
  } 
}

This script will tell you what the problem is. As Fabio writes, you'll probably see get the MySQL reported starting at the point where a space is expected. (You might need to add some br tags in this example if you want to use it on your website.)

Mark
  • 2,380
  • 11
  • 29
  • 49