解析错误:第67行的C:\ xampp \ htdocs \ vgdb \ csearch.php中的语法错误,意外结束文件,期望函数(T_FUNCTION)或const(T_CONST)

时间:2017-04-21 20:32:16

标签: php mysql oop mysqli

我的网站上有一个搜索栏。当我点击搜索按钮时,我收到上述错误。我不是一个高级编码器,所以我不知道如何解决它。我在网上搜索过,有这个问题的解决方案,但它们不适合我。这是一个项目,几天后到期,所以我很感激我能得到的任何帮助。这也是我在这里的第一篇文章,所以请放轻松。谢谢

csearch.php

<?php
/**
*Performs a search
*
* This class is used to perform search function in a MySQL database
*
*/
class search {
  /**
  * MySQLi connection
  *@access private
  *@var object
  */
  private $mysqli;

  /**
  * Constructor
  *
  * This sets up the class
  */

  public function __construct(){
    // Connect to the database and store in $mysqli property_exists
    $this->connect();
   }
  /**
  * Database connection
  *
  * This connects to the database
  */
  private function connect() {
    $this->mysqli = new mysqli( 'localhost', 'root', '', 'games');
    }

  /**
  * Search routine
  *
  * Performs a search
  *
  * @param string $search_term The search term
  *
  *
  */
  public function search($search_term) {
    // Sanatize the search term to prevent injection attacks
    $sanitized = $this->mysqli->real_escape_string($search_term);

    // Run the query
    $query = $this->mysqli->query("
    SELECT title
    FROM games
    WHERE title LIKE '%($sanitized)%'
    OR body LIKE '%($sanitized)%'
    ");

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

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

的search.php

<?php

$search_results= "";

//check if search data was submitted
if ( isset( $_GET['s'] ) ) {

  // Include the search class
  require_once( dirname( __FILE__ ) . '/csearch.php' );

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

  // Store search term into a variable
  $search_term = $_GET['s'];

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

}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta hhtp-equiv="X-UA-Compatible" content="IE-edge">
  <title>VGDB | SEARCH</title>
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="../vgdb/css/main.css">
  <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-
awesome.min.css" rel="stylesheet" integrity="sha384-
wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" 
crossorigin="anonymous">
  <link href='https://fonts.googleapis.com/css?family=Hind:600,400' 
rel='stylesheet'>
</head>
<body>
<?php include 'header.php';?>

<div class="container">
  <div class="col-md-5 col-md-offset-1">
    <div class="row">
      <div class="col-md-6">
    <div class="search">
      <h1>50,000 games and counting...</h1>
      <form action="" method="get">
        <input type="search" name="s" placeholder="Search for games..." 
results="5" value="<?php echo $search_term; ?>">
        <input type="submit" value="Search">
      </div>
    </div>
  </div>
</div>
</div>

</form>
<?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->title; ?></p>
      </div>
    <?php endforeach; ?>
  </div>
<?php endif; ?>

</body>
</html>
<?php include 'footer.php';?>

1 个答案:

答案 0 :(得分:0)

功能public function search($search_term)在第66行关闭。您需要添加另一个}以关闭class search

这是格式不佳的结果。请确保整齐地格式化您的代码,以避免这样的小错误。 :)

有时它是导致应用程序崩溃的最小的事情,有时候很难找到最小的东西......