Php 无法从PDO数据库类实例化对象

Php 无法从PDO数据库类实例化对象,php,mysql,pdo,Php,Mysql,Pdo,我为我的DB连接设置了以下代码,但是我无法从另一个.php文件(在另一个目录中)创建到数据库的连接: mydatabase.php文件: <?php ini_set('display_errors', 'On'); public class Database { public function __construct() { $this->dsn = 'mysql:host=xxx;dbname=xxx';

我为我的DB连接设置了以下代码,但是我无法从另一个.php文件(在另一个目录中)创建到数据库的连接:

mydatabase.php文件:

<?php

    ini_set('display_errors', 'On');

    public class Database {

        public function __construct() {
            $this->dsn = 'mysql:host=xxx;dbname=xxx';
            $this->username = 'xxx';
            $this->password = 'xxx';   
        }

        public function __construct($dsn, $username, $password) {
            $this->dsn = $dsn;
            $this->username = $username;
            $this->password = $password;
        }

        public function db_connect() {
            try {
                $database = new PDO($this->dsn , $this->username, $this->password);
                return $database;
            } catch(PDOException $e) {
                echo $e->getMessage();
            }
        }

        public function run_query($database, $query){            
            try {
                $result = $database->prepare($query);
                $result->execute();
                return $result;
            } catch (Exception $e) {
                echo $e->getMessage();
                die();
            }
        }

    }

?>
此文件的目录为currentdirectory/page.php


我一直在寻找一个错误已经有一段时间了,我看不出我做错了什么。关于PDO-DB类的其他问题对我也没有多大帮助。提前感谢您的帮助

您需要对
数据库.php
文件进行一些更改,因此您的文件代码应该是:

<?php

    ini_set('display_errors', 'On');

    class Database{

        public function __construct(){
            $this->dsn = 'mysql:host=xxx;dbname=xxx';
            $this->username = 'xxx';
            $this->password = 'xxx';
        }

        public function db_connect() {
            try {
                $database = new PDO($this->dsn , $this->username, $this->password);
                return $database;
            } catch(PDOException $e) {
                echo $e->getMessage();
            }
        }

        public function run_query($database, $query){            
            try {
                $result = $database->prepare($query);
                $result->execute();
                return $result;
            } catch (Exception $e) {
                echo $e->getMessage();
                die();
            }
        }

    }

?>

public
仅适用于变量/函数。它不适用于

您需要对
数据库.php
文件进行一些更改,因此您的文件代码应该是:

<?php

    ini_set('display_errors', 'On');

    class Database{

        public function __construct(){
            $this->dsn = 'mysql:host=xxx;dbname=xxx';
            $this->username = 'xxx';
            $this->password = 'xxx';
        }

        public function db_connect() {
            try {
                $database = new PDO($this->dsn , $this->username, $this->password);
                return $database;
            } catch(PDOException $e) {
                echo $e->getMessage();
            }
        }

        public function run_query($database, $query){            
            try {
                $result = $database->prepare($query);
                $result->execute();
                return $result;
            } catch (Exception $e) {
                echo $e->getMessage();
                die();
            }
        }

    }

?>

public
仅适用于变量/函数。它不适用于

公共
私有
受保护
用于类方法和/或属性,而不用于类本身。
您不应该有两个构造函数或两个同名函数,您将得到一个致命错误“cannot redecaler…”

请参见下面的示例。
它为dsn组件、pdo对象本身和pdo语句使用私有属性。
您可以在方法本身中返回这些,以便链接它们

<?php

class Database {

    private $host;
    private $username;
    private $password;
    private $database;

    private $pdo;
    private $stmt;


    public function __construct($host,$user,$pass,$db) {
        $this->host = $host;
        $this->username = $user;
        $this->password = $pass;
        $this->database = $db;

        $dsn = 'mysql:dbname=' . $this->database .';host=' . $this->host;

        try {

            $this->pdo = new PDO($dsn, $this->username, $this->password);

        } catch (PDOException $e) {
            echo 'Connection failed: ' . $e->getMessage();
        }

    }

    public function query($query){
        $this->stmt = $this->pdo->prepare($query);
        return $this;
    }

    public function getResults(){
        $this->stmt->execute();
        return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
    }

}

public
private
protected
用于类方法和/或属性,而不是类本身。
您不应该有两个构造函数或两个同名函数,您将得到一个致命错误“cannot redecaler…”

请参见下面的示例。
它为dsn组件、pdo对象本身和pdo语句使用私有属性。
您可以在方法本身中返回这些,以便链接它们

<?php

class Database {

    private $host;
    private $username;
    private $password;
    private $database;

    private $pdo;
    private $stmt;


    public function __construct($host,$user,$pass,$db) {
        $this->host = $host;
        $this->username = $user;
        $this->password = $pass;
        $this->database = $db;

        $dsn = 'mysql:dbname=' . $this->database .';host=' . $this->host;

        try {

            $this->pdo = new PDO($dsn, $this->username, $this->password);

        } catch (PDOException $e) {
            echo 'Connection failed: ' . $e->getMessage();
        }

    }

    public function query($query){
        $this->stmt = $this->pdo->prepare($query);
        return $this;
    }

    public function getResults(){
        $this->stmt->execute();
        return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
    }

}

你得到了什么错误?技术上不要创建多个构造函数。它没有在all@hardiksolanki我也想到了,但是在删除了一个没有参数的构造函数之后,它仍然不会执行。你得到了什么错误?从技术上讲,不要创建多个构造函数。它不会在all@hardiksolanki我也想到了,但是删除了一个没有参数的后,它仍然不会执行。谢谢!谢谢你!爱被解释得很好的人;-)谢谢谢谢你!爱被解释得很好的人;-)
// include the file

$db = new Database('localhost','root','','db_name');

print_r($db->query('select * from my_table limit 10')->getResults());