创建db类的实例时出现php错误

时间:2018-04-17 13:46:23

标签: php

所以我创建了一个用于处理sql select和connection东西的类。当我在索引页面中创建类的实例时,它会抛出错误

继承了db类代码

include "includes/config.php";

 class database {
  protected static $connection; // variable to hold the mysqli connection


 protected function connect(){
  if (!isset(self::$connection)){ // if the connection variable is not set
      self::$connection = new mysqli(SERVER_NAME, USERNAME, PASSWORD, DB_NAME); // set the connection
  }
  if (self::$connection === false){ //in case connection failed return false
    return false;
  }
  else {
    return self::$connection; // returns the connection
  }
}



protected function query($query){ // public function to take a sql query
  $connection = $this->connect(); // calls the connect function to create a connection to the database
  $result = $connection->query($query); // puts the query into the result variable
  return $result; //returns the result
}



public function select($query){
  $rows = array();
  $result = $this->query($query);
  if($result === false){
    return false;
  }
  while($row = $result->fetch_assoc()){
        $rows[] = $row;
        }
    return $rows;
}



public function error(){
  $connection = $this->connect();
  return $connection->error;
   }
 }
 ?>

当我尝试创建类实例

时,错误来自第7行的索引页面
<?php

include 'view/header.php';
include 'includes/connect.php';
include 'inckudes/config.php'

$db = new database();

$sql = "SELECT `first_name`, `last_name` FROM `pratts_db`
      WHERE `first_name` = `clive`;"

$result = $db->select($sql);

if (sizeof($result) >= 1){
echo "query successful";
}

include 'view/footer.php';

?>

确切的错误如下

  

解析错误:语法错误,意外&#39; $ db&#39; (T_VARIABLE)in   第7行的C:\ wamp64 \ www \ final \ index.php

0 个答案:

没有答案