View Single Post
Old Mar 26th, 2008, 1:58 PM   #5
big_k105
PFO Founder

 
big_k105's Avatar
 
Join Date: Mar 2004
Location: Fargo, ND
Posts: 1,603
Rep Power: 10 big_k105 is on a distinguished road
Send a message via AIM to big_k105 Send a message via MSN to big_k105 Send a message via Yahoo to big_k105
Re: Simple PHP Search Script

php Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <title>arnack.com Gaming Wesbite Database</title>
  4. </head>
  5. <body>
  6. <h2>Search</h2>
  7. <form name="search" method="post" action="<?=$PHP_SELF?>">
  8. Seach for: <input type="text" name="find" /> in
  9. <select NAME="field">
  10. <option VALUE="fname">First Person Shooters</option>
  11. <option VALUE="lname">Role PLaying Games</option>
  12. <option VALUE="info">Real Time Strategy</option>
  13. </select>
  14. <input type="hidden" name="searching" value="yes" />
  15. <input type="submit" name="search" value="Search" />
  16. </form>
  17. <?
  18. $searching = $_POST['searching'];
  19. $find = $_POST['find'];
  20. //This is only displayed if they have submitted the form
  21. if ($searching =="yes") {
  22. echo "<h2>Results</h2><p>";
  23.  
  24. //If they did not enter a search term we give them an error
  25. if ($find == "") {
  26. echo "<p>You forgot to enter a search term";
  27. exit;
  28. }
  29.  
  30. // Otherwise we connect to our Database
  31. mysql_connect("localhost", "arnackco_Arnack", "oompa123") or die(mysql_error());
  32. mysql_select_db("arnackco_search") or die(mysql_error());
  33.  
  34. // We preform a bit of filtering
  35. $find = strtoupper($find);
  36. $find = strip_tags($find);
  37. $find = trim ($find);
  38.  
  39. //Now we search for our search term, in the field the user specified
  40. $data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'");
  41. if ($data) {
  42. //And we display the results
  43. while($result = mysql_fetch_array( $data )) {
  44. echo $result['fname'];
  45. echo " ";
  46. echo $result['lname'];
  47. echo "<br>";
  48. echo $result['info'];
  49. echo "<br>";
  50. echo "<br>";
  51. }
  52.  
  53. //This counts the number or results - and if there wasn't any it gives them a little message explaining that
  54. $anymatches=mysql_num_rows($data);
  55. if ($anymatches == 0) {
  56. echo "Sorry, but we can not find an entry to match your query<br><br>";
  57. }
  58.  
  59. //And we remind them what they searched for
  60. echo "<b>Searched For:</b> " .$find;
  61. } else {
  62. echo "Error while sending query";
  63. }
  64. }
  65. ?>
  66. </body>
  67. </html>

Give this a try, I didn't test it though. All I did was add an if statement to check the $data variable before going through the results. I think you could also try changing this line $data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'"); to $data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'") or die("Query Error");
__________________
BIG K aka Kyle
Programming Forums
Kyle K Online

Please do not PM or email me programming questions. Post them in the forums instead.
big_k105 is online now   Reply With Quote