为什么在第二个函数调用中PDO对象为NULL?

时间:2019-03-17 05:06:10

标签: php

我用Object静态方法建立了数据库连接。然后,每个从数据库获取数据的函数将首先连接数据库。但是,在第二个函数调用“ get_us_states()”中,var_dump($ db)为“ NULL”,但是在第一个函数调用“ get_para_from_table_bank()”中,var_dump($ db)为“ object(PDO)#1(0 ){}”。某人能知道为什么吗?

代码在下面。

class Database_connection {
        private static $dsn = 'mysql:host=localhost;dbname=xxx';

        private static $user = 'xxx';

        private static $pw = 'xxx';

        private static $option = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);

        private static $db;

        public function __construct() {}

        public static function getDB () {
            if (!isset(self::$db)) {
                try {
                    self::$db = new PDO(self::$dsn,
                                        self::$user,
                                        self::$pw,
                                        self::$option);
                } catch (PDOException $e) {
                    $error_message = $e->getMessage();
                    include('../errors/database_error.php');
                    exit();
                }
                return self::$db;
            }
        }
    }


    function get_para_from_table_bank(){
        $db = Database_connection::getDB();
        $query = 'SELECT * FROM form_content_bank WHERE current_form = 1 ORDER BY orders';

        try {
            $statement = $db->prepare($query);
            $statement->execute();
            $results = $statement->fetchAll();
            $statement->closeCursor();
            return $results;
        } catch (PODException $e) {
            $error_message = $e->getMessage();
            include('../errors/database_error.php');
            exit();
        } 
    }


    function get_us_states(){
        $db = Database_connection::getDB ();

        $query = "SELECT * FROM us_states ORDER BY id";

        try {
            $statement = $db->prepare($query);
            $statement->execute();
            $us_states = $statement->fetchAll();
            $statement->closeCursor();
            return $us_states;
        } catch (PODException $e) {
            $error_message = $e->getMessage();
            include('../errors/database_error.php');
            exit();
        }
    }


    function show_form(){
        //get form content from database
        $form_contents = get_para_from_table_bank();
           ***var_dump($db) in "get_para_from_table_bank()" is "object(PDO)#1 (0) { }"***
        $us_states = get_us_states();
           ***var_dump($db) in "get_us_states()" is "NULL"***

         ... (other code)
     }

     show_form();

在调用show_form函数之后,总是显示“致命错误:未捕获的错误:在null上调用成员函数prepare(),而我在调用” get_us_states()”时发现,$ db为NULL。

某人能给我一些建议吗?

1 个答案:

答案 0 :(得分:0)

getDB函数缺少其他情况。 首次调用getDB时,没有数据库实例。它会在bolck中进入,但在第二个调用中会返回null

public static function getDB () {
        if (!isset(self::$db)) {
            try {
                self::$db = new PDO(self::$dsn,
                                    self::$user,
                                    self::$pw,
                                    self::$option);
            } catch (PDOException $e) {
                $error_message = $e->getMessage();
                include('../errors/database_error.php');
                exit();
            }
            return self::$db;
        }
        else return self::$db;
    }