致命错误:未捕获错误:在 X:\x\x\x\class\auth.php:29 中的 null 上调用成员函数prepare() 堆栈跟踪:#0 x:\x\x\index.php(8 ): Auth->register_user() #1 {main} 在第 29 行的 x:\x\x\x\class\auth.php 中抛出
这是数据库连接文件
class getDB {
protected $conn;
public $db_host = 'localhost';
public $db_user = 'root';
public $db_pass = '';
public $db_name = 'f_base';
public function getConn() {
try {
$this->conn = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", $this->db_user, $this->db_pass);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch ( PDOException $e ) {
die ('<h1>ERROR:</h1><h2>'.$e->getMessage().'</h2>');
}
}
// TODO: Dodati funkcije koje ce moci da ucitaju databazu da bi se prikazalo nesto iz databaze
}
该类用于注册
class Auth {
public function __construct () {
$connection = new getDB();
$this->conn = $connection->getConn();
return $this->conn;
}
public function register_user ( $username, $email, $password, $r_date ) {
$reg_user = $this->conn->prepare("INSERT INTO korisnici ( username, email, password, r_date ) VALUES ( ?, ?, ?, ? )"); // THIS IS LINE I AM GETTING ERROR
$reg_user->execute( array( $username, $email, $password, $r_date ) );
}
}
我收到错误的这一行
$reg_user = $this->conn->prepare("INSERT INTO korisnici ( username, email, password, r_date ) VALUES ( ?, ?, ?, ? )"); // THIS IS LINE I AM GETTING ERROR
米琪卡哇伊