Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.97 KB | None | 0 0
  1. <?php
  2.  
  3. /* Database info */
  4. $dbhost = 'localhost';
  5. $dbuser = 'root';
  6. $dbpass = '';
  7. $dbname = 'test';
  8.  
  9. /* Instantiate new mysqli object */
  10. $conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
  11.  
  12. /* Test mysql connection */
  13. if ($conn->connect_errno) {
  14.     echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
  15.     exit;
  16. }
  17.  
  18. /* Get search string */
  19. $search = $_POST['query'];
  20.  
  21. /* Remove multiple whitespaces from the search string */
  22. $search = preg_replace('/\s+/', ' ',$search);
  23.  
  24. /* Split search string into array for each word */
  25. $words = explode(' ', $search);
  26.  
  27. /* Stop words array */
  28. $stopWords = array('is','take','that');
  29.  
  30. /* Remove any duplicate entries in words array */
  31. $words = array_unique($words);
  32.  
  33. /* Remove any stop words from the words array */
  34. foreach($stopWords as $word){
  35.     if(($key = array_search($word, $words)) !== false) {
  36.         unset($words[$key]);
  37.     }
  38. }
  39.  
  40. /* Check if there are any words left to search for */
  41. if(empty($words)){
  42.     echo "Failed to perform search: Please avoid using common words like 'is', 'take' or 'that'.";
  43.     exit;
  44. }
  45.  
  46. /* Start query */
  47. $query = 'SELECT q_id, question, username, q_date FROM questions WHERE (question ';
  48.  
  49. /* Loop through search words and build the query */
  50. for($i = 0; $i < count($words); $i++){
  51.     if($i == (count($words) - 1)){
  52.         $query .= 'LIKE ?)';
  53.     } else {
  54.         $query .= 'LIKE ? OR question ';
  55.     }
  56. }
  57.  
  58. /* Build bind parameters array */
  59. $a_param_type = array();
  60.  
  61. for($i = 0; $i < count($words); $i++){
  62.     $a_param_type[] = 's';
  63. }
  64.  
  65. $a_params = array();
  66.  
  67. $param_type = '';
  68. $n = count($a_param_type);
  69. for($i = 0; $i < $n; $i++) {
  70.     $param_type .= $a_param_type[$i];
  71. }
  72.  
  73. /* with call_user_func_array, array params must be passed by reference */
  74. $a_params[] = & $param_type;
  75.  
  76. for($i = 0; $i < $n; $i++) {
  77.     /* with call_user_func_array, array params must be passed by reference */
  78.     $a_params[] = '%'.$words[$i].'%';
  79. }
  80.  
  81. /* Prepare statement */
  82. $stmt = $conn->prepare($query);
  83. if($stmt === false) {
  84.     trigger_error('Wrong SQL: ' . $query . ' Error: ' . $conn->errno . ' ' . $conn->error, E_USER_ERROR);
  85. }
  86.  
  87. /* use call_user_func_array, as $stmt->bind_param('s', $param); does not accept params array */
  88. call_user_func_array(array($stmt, 'bind_param'), refValues($a_params));
  89.  
  90. function refValues($arr){
  91.     if (strnatcmp(phpversion(),'5.3') >= 0) //Reference is required for PHP 5.3+
  92.     {
  93.         $refs = array();
  94.         foreach($arr as $key => $value)
  95.             $refs[$key] = &$arr[$key];
  96.         return $refs;
  97.     }
  98.     return $arr;
  99. }
  100.  
  101. /* Execute statement */
  102. $stmt->execute();
  103.  
  104. /* Fetch result */
  105. $res = $stmt->get_result();
  106.  
  107. /* Count rows */
  108. $cnt = $res->num_rows;
  109.  
  110. if($cnt > 0){
  111.     while($row = $res->fetch_array(MYSQLI_ASSOC)) {
  112.         $question = $row['question'];
  113.  
  114.         echo ('<a href="totalqs.php?q_id=' . $row['q_id'] .'" >' . $question .'Asked by:'.' '.$row['username'].' '.$row['q_date'] .' </a>'  . '<br>');
  115.     }
  116. } else {
  117.     echo "No results found";
  118. }
  119.  
  120. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement