我无法连接到我的数据库。 PHP 和 MySQL

I can't connect with my database. PHP and MySQL

我只是 PHP 的初学者。而且我无法连接我的数据库。这是我的代码:

<?php
require 'config.php';

class db_class {
    public $host        =db_host;
    public $username    =db_username;
    public $password    =db_password;
    public $db_name =db_name;
    public $conn;
    public $error;

    private function _construct () 
    {
        $this-> conn (); 
    }

    private function connect () {
        $this->conn=new mysqli ($this->host, $this->username, $this->password, $this->db_name);
        if ($this->conn) {
            $this-> error ="Fatal Error: Can't connect to lib database" .$this->conn->connect_error;
        return false;
        }
    }

    public function save ($username, $password, $first_name, $middle_name, $last_name, $student_id) 
    {
        $reg = $this->conn->prepare ("INSERT INTO registration (username, password, first_name, middle_name, last_name, student_id) VALUES (?, ?, ?, ?, ?, ?)") or die ($this->conn->error);
        $reg->bind_param ("sssss",$username,$password,$first_name,$middle_name,$last_name, $student_id);
        if  ( $reg->execute()) {
            $reg->close();
            $this->conn->close();
            return true;
        }
    }
}
?>

因此,当我单击“提交”按钮时,没有任何反应。请帮忙。谢谢!

正如@RiggsFolly 所指出的,您的 construct() 函数没有执行任何功能。你打错了。应该是 __construct() 方法 conn() 不存在。您应该调用 connect() 函数。占位符的数量也与变量不匹配。应该是6

private function __construct() #you have a typo.should be __construct()
    {
        $this->connect(); #call connect() function
    }

$reg->bind_param ("ssssss",$username,$password,$first_name,$middle_name,$last_name, $student_id);#six placeholders for six variables

您似乎正在尝试使用 mysqli 函数和 pdo 函数。 其余的代码似乎只是 PDO。尝试将连接字符串更改为 pdo 方法。

而不是

$this->conn=new mysqli ($this->host, $this->username, $this->password, $this->db_name);

尝试

$this->conn = new PDO("mysql:host=".$this->host.";dbname=".$this->db_name, $this->username, $this->password);