PHP未定义变量:search_results

时间:2015-05-13 11:53:00

标签: php search mysqli

我试图制作一个简单的php搜索引擎 我使用过两个文件 1- search.php

    <?php
      //Check if search data was submitted
      if ( isset( $_GET['s'] ) ) {

  // Include the search class
  require_once 'class-search.php';

  // Instantiate a new instance of the search class
  $search = new search();

  // Store search term into a variable
  $search_term = htmlspecialchars($_GET['s'], ENT_QUOTES);

  // Send the search term to our search class and store the result
  $search_results = $search->search($search_term);

}
?>
<!DOCTYPE html>
<html>
  <head>
    <title>Search</title>
  </head>
  <body>
    <h1>Search</h1>
    <div class="search-form">
      <form action="" method="get">
        <div class="form-field">
          <label for="search-field">Search</label>
          <input type="search" name="s" placeholder="Enter your search term..." results="5" value="<?php echo $search_term; ?>">
          <input type="submit" value="Search">
        </div>
      </form>
    </div>
    <?php 
           if ($search_results) : ?>
    <div class="results-count">
      <p><?php echo $search_results['count']; ?> results found</p>
    </div>
    <div class="results-table">
      <?php foreach ( $search_results['results'] as $search_result ) : ?>
      <div class="result">
        <p><?php echo $search_result->CompanyName; ?></p>
      </div>
      <?php endforeach; ?>
    </div>
    <div class="search-raw">
      <pre><?php print_r($search_results); ?></pre>
    </div>
    <?php endif; ?>
  </body>
</html>

2-另一个是class-search.php

<?php

class search {
  /**
   * MySQLi connection
   * @access private
   * @var object
   */
  private $mysqli;

  /**
   * Constructor
   *
   * This sets up the class
   */
  public function __construct() {
    // Connect to our database and store in $mysqli property
    $this->connect();
  }
  /**
   * Database connection
   * 
   * This connects to our database
   */
  private function connect() {
    $this->mysqli = new mysqli( 'localhost', 'username', 'password', 'db' );
  }

  /**
   * Search routine
   * 
   * Performs a search
   * 
   * @param string $search_term The search term
   * 
   * @return array/boolen $search_results Array of search results or false
   */
  public function search($search_term) {
    // Sanitize the search term to prevent injection attacks
    $sanitized = $this->mysqli->real_escape_string($search_term);

    // Run the query
    $query = $this->mysqli->query("
      SELECT *
      FROM suppliers
      WHERE CompanyName LIKE '%{$sanitized}%'
      OR Service LIKE '%{$sanitized}%'
    ");

    // Check results
    if ( ! $query->num_rows ) {
      return false;
    }




    // Loop and fetch objects
    while( $row = $query->fetch_object() ) {
      $rows[] = $row;
    }

    // Build our return result
    $search_results = array(
      'count' => $query->num_rows,
      'results' => $rows,
    );

  return $search_results;
  } 
}
?>

问题我一直收到这个通知  未定义的变量:search.php中第36行的search_results 当我输入空字符串时,它会检索整个数据库。

0 个答案:

没有答案