Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* Database info */
- $dbhost = 'localhost';
- $dbuser = 'root';
- $dbpass = '';
- $dbname = 'test';
- /* Instantiate new mysqli object */
- $conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
- /* Test mysql connection */
- if ($conn->connect_errno) {
- echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
- exit;
- }
- /* Get search string */
- $search = $_POST['query'];
- /* Remove multiple whitespaces from the search string */
- $search = preg_replace('/\s+/', ' ',$search);
- /* Split search string into array for each word */
- $words = explode(' ', $search);
- /* Stop words array */
- $stopWords = array('is','take','that');
- /* Remove any duplicate entries in words array */
- $words = array_unique($words);
- /* Remove any stop words from the words array */
- foreach($stopWords as $word){
- if(($key = array_search($word, $words)) !== false) {
- unset($words[$key]);
- }
- }
- /* Check if there are any words left to search for */
- if(empty($words)){
- echo "Failed to perform search: Please avoid using common words like 'is', 'take' or 'that'.";
- exit;
- }
- /* Start query */
- $query = 'SELECT q_id, question, username, q_date FROM questions WHERE (question ';
- /* Loop through search words and build the query */
- for($i = 0; $i < count($words); $i++){
- if($i == (count($words) - 1)){
- $query .= 'LIKE ?)';
- } else {
- $query .= 'LIKE ? OR question ';
- }
- }
- /* Build bind parameters array */
- $a_param_type = array();
- for($i = 0; $i < count($words); $i++){
- $a_param_type[] = 's';
- }
- $a_params = array();
- $param_type = '';
- $n = count($a_param_type);
- for($i = 0; $i < $n; $i++) {
- $param_type .= $a_param_type[$i];
- }
- /* with call_user_func_array, array params must be passed by reference */
- $a_params[] = & $param_type;
- for($i = 0; $i < $n; $i++) {
- /* with call_user_func_array, array params must be passed by reference */
- $a_params[] = '%'.$words[$i].'%';
- }
- /* Prepare statement */
- $stmt = $conn->prepare($query);
- if($stmt === false) {
- trigger_error('Wrong SQL: ' . $query . ' Error: ' . $conn->errno . ' ' . $conn->error, E_USER_ERROR);
- }
- /* use call_user_func_array, as $stmt->bind_param('s', $param); does not accept params array */
- call_user_func_array(array($stmt, 'bind_param'), refValues($a_params));
- function refValues($arr){
- if (strnatcmp(phpversion(),'5.3') >= 0) //Reference is required for PHP 5.3+
- {
- $refs = array();
- foreach($arr as $key => $value)
- $refs[$key] = &$arr[$key];
- return $refs;
- }
- return $arr;
- }
- /* Execute statement */
- $stmt->execute();
- /* Fetch result */
- $res = $stmt->get_result();
- /* Count rows */
- $cnt = $res->num_rows;
- if($cnt > 0){
- while($row = $res->fetch_array(MYSQLI_ASSOC)) {
- $question = $row['question'];
- echo ('<a href="totalqs.php?q_id=' . $row['q_id'] .'" >' . $question .'Asked by:'.' '.$row['username'].' '.$row['q_date'] .' </a>' . '<br>');
- }
- } else {
- echo "No results found";
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement