dqroc48068 2017-03-07 08:40
浏览 40
已采纳

未捕获错误:调用成员函数prepare()on null error [duplicate]

This question already has an answer here:

I know there were a lot of answers related to this error, but I still don't know how to solve it... I'm trying to make the database connection, which would connect to the database and insert user's entered values in it and i got this error. I've created 2 files (with different classes):

Here is a connection file:

<?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();
?>

And here is users.php file:

<?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();
?>

I guess there are some mistakes in code structure, but I'm just learning, so. The code line which throws the error 18 line in users.php file:

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

Please someone tell me where I am doing a mistake, thank you for any help.

</div>
  • 写回答

3条回答 默认 最新

  • du21064 2017-03-07 08:42
    关注

    You just have somes mistakes in your code. Try to use this lines :

    Connection file :

    <?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 file :

    <?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();
    

    Explanations :

    • You have to pass the $connection variable to your users class (or import it with global $connection;)
    • Your connection file has to make visible the dbh property, otherwise you will not be able to make any query on your database
    • PDO prepare() method is waiting for a query in first argument
    • You don't need to pass an array to execute() method if you already have binded your values before
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 Mac(标签-IDE|关键词-File) idea
  • ¥15 潜在扩散模型的Unet特征提取
  • ¥15 iscsi服务无法访问,如何解决?
  • ¥15 感应式传感器制作的感应式讯响器