我可以在任何页面中包含此类以实现数据库连接吗?

时间:2014-05-03 00:24:16

标签: php mysql pdo connection

我想知道如果我只是包含它们,这两个类是否允许我建立与我的数据库的连接。这些是类:


DBconn.php

<?php

class DBconn {
    protected $dbname;
    protected $dbuser;
    protected $dbpassword;
    protected $dbhost;
    protected $connection;

    public function __construct($dbhost, $dbname, $dbuser, $dbpass) 
    {
        $this->dbname = $dbname;
        $this->dbhost = $dbhost;
        $this->dbuser = $dbuser;
        $this->dbpass = $dbpass;
        $this->connect();
    }

    public function getConnection()
    {
        return $this->connection;
    }

    protected function connect()
    {
        $this->connection = new PDO("mysql:host={$this->dbhost};dbname={$this->dbname}", $this->dbuser, $this->dbpass);
    }
}
?>


dblogin.php

<?php

$db = new DBconn('localhost','phpproject','carl','pdt1848?')

?>

0 个答案:

没有答案