4 Replies - 868 Views - Last Post: 04 December 2013 - 11:55 PM Rate Topic: -----

#1 dragon_12dk   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-December 13

Error in my PHP search script?

Posted 04 December 2013 - 09:24 PM

Hello, I have been getting the following error on my search engine but I cant figure out why,

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/a6538015/public_html/Search Engine/search.php on line 32

here is the code I have:

<?php
 
$button = $_GET ['submit'];
$search = $_GET ['search']; 
 
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("mysql3.000webhost.com","a6538015_test","hd355044");
mysql_select_db("a6538015_test");
 
$search_exploded = explode (" ", $search);
 
foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="keywords LIKE '%$search_each%'";
else
$construct .="AND keywords LIKE '%$search_each%'";
 
}
 
$construct ="SELECT * FROM searchengine 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. for example: If you want to search 'how to create a website'
then use general keyword like 'create' 'website'</br>2. Try different words with similar
 meaning</br>3. Please check your spelling";
else
{
echo "$foundnum results found !<p>";
 
while($runrows = mysql_fetch_assoc($run))
{
$title = $runrows ['title'];
$desc = $runrows ['description'];
$url = $runrows ['url'];
 
echo "
<a href='$url'><b>$title</b></a><br>
$desc<br>
<a href='$url'>$url</a><p>
";
 
}
}
 
}
}
 
?>



thank you for your help :)/>

Is This A Good Question/Topic? 0
  • +

Replies To: Error in my PHP search script?

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12772
  • View blog
  • Posts: 45,965
  • Joined: 27-December 08

Re: Error in my PHP search script?

Posted 04 December 2013 - 09:26 PM

This error means that something probably went wrong with your query. Adding an or die(mysql_error(); clause after your mysql_query() function will give you more information as to the problem.

In all honesty, though, the mysql_*() family of functions are deprecated and should be avoided in favor of PDO or MySQLi. You will want to use Prepared Statements to guard against SQL Injection attacks as well.
Was This Post Helpful? 0
  • +
  • -

#3 dragon_12dk   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-December 13

Re: Error in my PHP search script?

Posted 04 December 2013 - 09:44 PM

Ok, I added the
or die (MySQL_error(); 
and it did display more information :)
now it says

Parse error: syntax error, unexpected T_LOGICAL_OR in /home/a6538015/public_html/Search Engine/search.php on line 31
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12772
  • View blog
  • Posts: 45,965
  • Joined: 27-December 08

Re: Error in my PHP search script?

Posted 04 December 2013 - 09:46 PM

Output your query string and see what you get. That will tell you more. Also, you are taking values straight from $_GET and passing them into your query without sanitizing them. This is why you should migrate to prepared statements with PDO or MySQLi, as you won't have to worry with this problem. I have a hunch this is the root of your issue.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6916
  • View blog
  • Posts: 28,598
  • Joined: 12-December 12

Re: Error in my PHP search script?

Posted 04 December 2013 - 11:55 PM

View Postdragon_12dk, on 05 December 2013 - 04:44 AM, said:

Ok, I added the
or die (MySQL_error(); 
and it did display more information :)/>
now it says

Parse error: syntax error, unexpected T_LOGICAL_OR in /home/a6538015/public_html/Search Engine/search.php on line 31

That is because you are missing a closing bracket.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1