php Prepared Statement Search (Needs Improvement)


/ Published in: PHP
Save to your folder(s)

Im pretty new to oop php i have tried to create a php search function but im sure this needs alot of improvement. So add on :D


Copy this code and paste it in your HTML
  1. public function search($string)
  2. {
  3. if(strlen($string) > 40)
  4. {
  5. return "Woow too many words, please shorten your search.";
  6. }
  7. if(empty($string))
  8. {
  9. return "I'm a search, you type things in and I find. Please enter something and try again!";
  10. }
  11. if(strlen($string) <= 3)
  12. {
  13. return "Come on dude, you expect me to find summin with that? Type some more tags in!";
  14. }
  15.  
  16. $x=0;
  17. // Teh string could be multiple searches so explode::
  18. $string = explode(" ", $string);
  19.  
  20. foreach($string as $search)
  21. {
  22. $x++;
  23. if($x == 1)
  24. {
  25. @$sql .= "(blog_tags LIKE '%$search%')";
  26. }
  27. else
  28. {
  29. @$sql .= " OR (blog_tags LIKE '%$search%')";
  30. }
  31. }
  32.  
  33. $sql = "SELECT blog_tags FROM subarc_blog WHERE $sql LIMIT 40";
  34.  
  35. // TODO:: Count how many search results found::
  36. $stmt = $this->conn->prepare($sql);
  37. $stmt->execute();
  38.  
  39. $meta = $stmt->result_metadata();
  40. while($field = $meta->fetch_field())
  41. {
  42. $var = $field->name;
  43. $$var = null;
  44. $params[$var] = &$var;
  45. }
  46.  
  47. call_user_func_array(array($stmt,'bind_result'),$params);
  48.  
  49. while($stmt->fetch())
  50. {
  51. return $params;
  52. }
  53. $stmt->close();
  54. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.