Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. <?php
  2. $query = $_GET['query'];
  3. // gets value sent over search form
  4.  
  5. $min_length = 3;
  6. // you can set minimum length of the query if you want
  7.  
  8. if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
  9.  
  10. $query = htmlspecialchars($query);
  11. // changes characters used in html to their equivalents, for example: < to &gt;
  12.  
  13. $query = mysql_real_escape_string($query);
  14. // makes sure nobody uses SQL injection
  15.  
  16. $raw_results = mysql_query("SELECT * FROM articles
  17. WHERE (`title` LIKE '%".$query."%') OR (`text` LIKE '%".$query."%')") or die(mysql_error());
  18.  
  19. // * means that it selects all fields, you can also write: `id`, `title`, `text`
  20. // articles is the name of our table
  21.  
  22. // '%$query%' is what we're looking for, % means anything, for example if $query is Hello
  23. // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
  24. // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'
  25.  
  26. if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
  27.  
  28. while($results = mysql_fetch_array($raw_results)){
  29. // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
  30.  
  31. echo "<p><h3>".$results['title']."</h3>".$results['text']."</p>";
  32. // posts results gotten from database(title and text) you can also show id ($results['id'])
  33. }
  34.  
  35. }
  36. else{ // if there is no matching rows do following
  37. echo "No results";
  38. }
  39.  
  40. }
  41. else{ // if query length is less than minimum
  42. echo "Minimum length is ".$min_length;
  43. }
  44. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement