【问题标题】:Fatal error: Uncaught Error: Call to undefined function_name() in var/www致命错误:未捕获的错误:调用 var/www 中未定义的 function_name()
【发布时间】:2016-09-21 23:05:10
【问题描述】:

大家好,我正在使用类进行 PDO 连接后的插入过程。一切正常,连接和显示。但是当我在类中创建新函数并为插入过程键入命令时,我得到了这个错误行:

致命错误:未捕获的错误:调用 /var/www/html/test/index.php:29 中未定义的函数 db_​​connection_function() 堆栈跟踪:#0 /var/www/html/test/index.php(48 ): connection->add_member_to_table() #1 {main} 在第 29 行的 /var/www/html/test/index.php 中抛出

这个函数给我错误

public function add_member_to_table() {
    $this->query = db_connection_function()->prepare("INSERT INTO users(username, password) VALUES('onur', 'turali')");
    $this->query->execute();

    if($this->query == true) {
        echo "Member registered";
    } else {
        echo "Error";
    }
}

我试过 $this-> connection_db_link.... 我想尝试输入函数名而不是 connection_db_link(连接到 mysql 的函数名)但我认为这没用。那么如何解决这个问题呢?

我的源代码:

<?php
    class connection{
        public $connection_db_link;
        public $db_host = "localhost";
        public $db_user = "root";
        public $db_pass = "Antalya07Ragnar";
        public $db_name = "test";

        public function db_connection_function(){
            try{
                $this -> connection_db_link = new PDO("mysql:host=$this->db_host;$this->db_name", $this->db_user, $this->db_pass);
                $this->connection_db_link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                return $this->connection_db_link;
            }catch(PDOException $e){
                echo "Error: ".$e->getMessage();
            }
        }

        public $query;

        public function add_member_to_table(){
            $this->query = db_connection_function()->prepare("INSERT INTO users(username, password) VALUES('onur', 'turali')");
            $this->query->execute();
            if($this->query == true){
                echo "Member registered";
            }else{
                echo "Error";
            }
        }

        public function display_connection(){
            if($this->connection_db_link == true){
                echo "Connection success";
            }
        }
    }

    $user = new connection;
    $user->db_connection_function();
    $user->display_connection();
    $user->add_member_to_table();
    ?>

【问题讨论】:

  • 它是你的类中的方法,它的$this-&gt;db_connection_function(),为什么不在构造函数中创建它,而是将它用作属性
  • 当我需要准备sql进程时,我总是使用函数进行连接。因为,我在准备命令之前调用了该函数。但是如果连接函数在另一个文件中,我使用构造函数。这不是我知道的好方法。但是它,使用类的PDO连接,它真的强迫我。
  • 您不应该在每次调用该函数时都创建新连接。您应该检查$this-&gt;connection_db_link 是否已设置,然后返回该变量而不是打开新连接。
  • 是的,合乎逻辑。但是按照你说的,我应该调用函数还是只调用sql进程的PDO值?

标签: php mysql pdo


【解决方案1】:

编辑:

在add_member_to_table()函数中,将db_connection_function()改为$this-&gt;db_connection_function()


在您尝试使用db_connection_function() 函数之前,您应该确认您已成功连接到数据库。

您的 PDO 连接语句缺少 dbname=,应该如下所示:

$this->connection_db_link = new PDO('mysql:host=$this->db_host;dbname=$this->db_name', $this->db_user, $this->db_pass);

确定之后,prepare 语句的工作方式如下:

$stmt = $dbh->prepare("INSERT INTO users (username, password) VALUES (:username, :password)");
$stmt->bindParam(':username', $username);
$stmt->bindParam(':password', $password);

// insert one row
$name = 'onur';
$value = 'turali';
$stmt->execute();

【讨论】:

  • 谢谢!我怎么没想到。我认为这仅适用于 post 方法。我摆脱了这种无知。再次感谢。
猜你喜欢
  • 2021-06-21
  • 1970-01-01
  • 2019-05-22
  • 2017-01-27
  • 2016-04-07
  • 2017-08-19
相关资源
最近更新 更多