未捕获的错误:在出现空错误时调用成员函数 prepare()

编程入门 行业动态 更新时间:2024-10-26 07:25:32
本文介绍了未捕获的错误:在出现空错误时调用成员函数 prepare()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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):

这是一个连接文件:

<?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 文件:

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

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

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.

推荐答案

您只是在代码中存在一些错误.尝试使用这一行:

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

连接文件:

<?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 文件:

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

说明:

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

更多推荐

未捕获的错误:在出现空错误时调用成员函数 prepare()

本文发布于:2023-11-27 19:36:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639334.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时调   错误   函数   成员   prepare

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!