致命错误:未捕获错误:在null上调用成员函数prepare()

时间:2017-12-01 19:14:15

标签: php

这是三个文件

config.php
functions.php
index.php

我收到错误"致命错误:未捕获错误:在null"上调用成员函数prepare()。我是PHP oop的新手。请帮帮我。

我在config.php中有代码

class DatabaseConnection {
   public function __construct() {
      try {
         $pdo = new PDO('mysqli:host=xxx;dbname=xxx', 'root', '');
      } catch(PDOException $e) {
          exit('Database error');
      }
   }
}

functions.php代码见下文

require "config.php";

class LoginRegistration {
function __constructor() {
  $database = new DatabaseConnection();
}

public function registerUser($username, $password, $name, $email, $website) {
  global $pdo;
  $query = $pdo->prepare("SELECT id FROM users WHERE username = ? AND email = ?");
  exit();
  $query->execute(array($username, $email));
  $num = $query->rowCount();

  if($num == 0) {
    $query = $pdo -> prepare("INSERT INTO users (username, password, name, email, website) VALUES (?, ?, ?, ?, ?)");
    $query -> execute(array($username, $password, $name, $email, $website));
    return true;
  } else {
    print "Username and email already used.";
  }
}
}

你可以在下面找到index.php代码。

  require_once "functions.php";
  $user = new LoginRegistration();
    if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $name = $_POST['name'];
    $email = $_POST['email'];
    $website = $_POST['website'];

    $password = md5($password);
    $register = $user->registerUser($username, $password, $name, $email, $website);
    if($register) {
  echo "<div class='alert alert-success'>Register done <a href='login.php'>Click her</a> for login</div>";
} else {
  echo "<div class='alert alert-danger'>Username or Email already exists.</div>";
}
}

1 个答案:

答案 0 :(得分:1)

您不会从全局范围获取$pdo类中的DatabaseConnection变量。因此,您的$pdo变量基本上是null。您可以更改以下代码:

的config.php:

class DatabaseConnection {
   public function __construct() {
      try {
         return new PDO('mysqli:host=xxx;dbname=xxx', 'root', '');            
      } catch(PDOException $e) {
          exit('Database error');
      }
   }
}

然后:

class LoginRegistration {
    private $database

    function __construct() {
        $this->database = new DatabaseConnection();
    }

    public function registerUser($username, $password, $name, $email, $website) {

      $query = $this->database->prepare("SELECT id FROM users WHERE username = ? AND email = ?");
      $query->execute(array($username, $email));
      $num = $query->rowCount();

      ...

但是,我宁愿扩展DatabaseConnection类,而是在LoginRegistration类中实例化它。那就是:

的config.php:

class DatabaseConnection {
   protected $pdo;
   public function __construct() {
      try {
         $this->pdo = new PDO('mysqli:host=xxx;dbname=xxx', 'root', '');            
      } catch(PDOException $e) {
          exit('Database error');
      }
   }
}

然后从DatabaseConnection类扩展LoginRegistration

class LoginRegistration extends DatabaseConnection {
    // constructor is not needed anymore
    public function registerUser($username, $password, $name, $email, $website) {

      $query = $this->pdo->prepare("SELECT id FROM users WHERE username = ? AND email = ?");
      $query->execute(array($username, $email));
      $num = $query->rowCount();

      ...