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

时间:2017-03-07 08:40:27

标签: php mysql oop

我知道有很多与此错误有关的答案,但我仍然不知道如何解决它...我正在尝试建立数据库连接,这将连接到数据库并插入用户输入的它中的值,我得到了这个错误。我创建了2个文件(包含不同的类):

这是一个连接文件:

<?php
class Connection {
    // Setting Database Source Name (DSN)
 public function __construct() {
$dsn = 'mysql:host=localhost;dbname=employees';
// Setting options
$options = array (PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
// Making the connection to the database
try {
$this->dbh = new PDO($dsn, 'root', '', $options); 
}
catch (PDOException $e) {
$this->error = $e->getMessage();
        }
    }
}
$connection = new connection();
?>

这是users.php文件:

<?php
include 'connection.php';
class Users {
public $name;
public $surname;
public $employmentDate;
public function __construct()
{
if(isset($_POST['Submit'])) {
$this->name = $_POST['name'];
$this->surname = $_POST['surname'];
$this->employmentDate = $_POST['employmentDate'];
}
}
// Inserting users values to the database table
public function insertUserValues() {
 $stmt= 'INSERT INTO employee (name,surname,employment_date) VALUES (:name,:surname,:employmentDate)';
 $stmt = $this->dbh->prepare();
 $stmt->bindValue(':name',$name, PDO::PARAM_STR);
 $stmt->bindValue(':surname',$surname, PDO::PARAM_STR);
 $stmt->bindValue(':employmenDate',$employmentDate, PDO::PARAM_STR);
 $stmt->execute([$this->name,$this->surname,$this->employmentDate]);
}
}
$users = new Users();
$users->insertUserValues();
?>

我想代码结构中有一些错误,但我只是在学习,所以。在users.php文件中抛出错误18行的代码行:

$stmt = $this->dbh->prepare();

请有人告诉我我在哪里犯了错误,谢谢你的帮助。

3 个答案:

答案 0 :(得分:1)

你的代码中只有一些错误。尝试使用这一行:

连接文件:

<?php
class Connection {
    public $dbh;

    // Setting Database Source Name (DSN)
    public function __construct() {
        $dsn = 'mysql:host=localhost;dbname=employees';
        // Setting options
        $options = array (PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
        // Making the connection to the database
        try {
            $this->dbh = new PDO($dsn, 'root', '', $options); 
        }
        catch (PDOException $e) {
            $this->error = $e->getMessage();
        }
    }
}

$connection = new connection();

users.php文件:

<?php

include 'connection.php';
class Users {
    public $name;
    public $surname;
    public $employmentDate;
    public $connection;

    public function __construct($connection)
    {
        $this->connection = $connection;
        if(isset($_POST['Submit'])) {
            $this->name = $_POST['name'];
            $this->surname = $_POST['surname'];
            $this->employmentDate = $_POST['employmentDate'];
        }
    }

    // Inserting users values to the database table
    public function insertUserValues() {
        $query = 'INSERT INTO employee (name,surname,employment_date) VALUES (:name,:surname,:employmentDate)';
        $stmt = $this->connection->dbh->prepare($query);
        $stmt->bindValue(':name',$this->name, PDO::PARAM_STR);
        $stmt->bindValue(':surname',$this->surname, PDO::PARAM_STR);
        $stmt->bindValue(':employmentDate',$this->employmentDate, PDO::PARAM_STR);
        $stmt->execute();
    }
}   

$users = new Users($connection);
$users->insertUserValues();

解释:

  • 您必须将$ connection变量传递给您的用户类(或使用global $connection;导入它)
  • 您的连接文件必须使dbh属性可见,否则您将无法对数据库进行任何查询
  • PDO prepare()方法正在等待第一个参数
  • 中的查询
  • 如果您之前已经绑定了值,则不需要将数组传递给execute()方法

答案 1 :(得分:0)

您指的是课程范围之外的$this。你可能想要

$connection->dbh->prepare();

答案 2 :(得分:0)

您可以将连接类设置为单例:

class Connection {

    private static $instance = null;

    // Setting Database Source Name (DSN)
    public function __construct() {
       $dsn = 'mysql:host=localhost;dbname=employees';
      // Setting options
      $options = array (PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
     // Making the connection to the database
     try {
        $this->dbh = new PDO($dsn, 'root', '', $options); 
     } catch (PDOException $e) {
       $this->error = $e->getMessage();
        }
    }

    public static function __callStatic($name, $args) {
        if (self::$instance == null) {
             self::$instance = new self();
        }
        call_user_func_array([ self::$instance->dbh, $name], $args);
    }   

}

然后按如下方式使用:

 Connection::prepare(); //or connection::any_pdo_function