I have a PDO connection to database in index.php
file.
我在index.php文件中有一个到数据库的PDO连接。
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbname";
$dbh_conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$dbh_conn->exec("set names utf8");
$dbh_conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbh_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Almost all pages of my website need database. Also there is a few pages which don't need database, but because all pages pass from index.php
then that connection executes. I mean for all pages (even those ones which doesn't need database) there is a database connection.
几乎我网站的所有页面都需要数据库。还有一些页面不需要数据库,但因为所有页面都从index.php传递,然后该连接执行。我的意思是所有页面(甚至那些不需要数据库的页面)都有数据库连接。
Is that a bad thing? Should I change all my structure? Again, the most of the pages need database, just a few of them doesn't. So is what I do fine?
那是一件坏事?我应该改变我的所有结构吗?同样,大多数页面都需要数据库,其中只有少数没有。那我做得好吗?
1 个解决方案
#1
1
You need to create a singleton class for the DB connection, and not just run the code.
您需要为数据库连接创建一个单例类,而不仅仅是运行代码。
class DB {
private static $instance;
private $_dbh_conn = null;
public static function getInstance() {
if (null === static::$instance) {
static::$instance = new static();
}
return static::$instance;
}
protected function __construct() {
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbname";
$this->_dbh_conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$this->_dbh_conn->exec("set names utf8");
$this->_dbh_conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->_dbh_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public function &dbh_conn() {
return $this->_dbh_conn;
}
private function __clone() { }
private function __wakeup() { }
}
And when you include this file, you need to get the connection like this:
当您包含此文件时,您需要获得如下连接:
$dbh_conn = DB::getInstance()->dbh_conn();
Now you have the connection to the database in $dbh_conn
and you can use it just as you did up until now. The only difference is that now you can include this file, but it won't connect to the DB unless you use the Singleton class DB
and specifically requesting the DB connection (as I did in the above example). Additional benefit is that you're getting the same DB connection throughout the runtime of the script because you're using a design pattern of a singleton class to get the connection.
现在,您可以在$ dbh_conn中连接到数据库,并且可以像直到现在一样使用它。唯一的区别是现在你可以包含这个文件,但它不会连接到数据库,除非你使用Singleton类DB并特别请求数据库连接(就像我在上面的例子中所做的那样)。另外一个好处是,您在整个脚本运行时获得相同的数据库连接,因为您正在使用单例类的设计模式来获取连接。
#1
1
You need to create a singleton class for the DB connection, and not just run the code.
您需要为数据库连接创建一个单例类,而不仅仅是运行代码。
class DB {
private static $instance;
private $_dbh_conn = null;
public static function getInstance() {
if (null === static::$instance) {
static::$instance = new static();
}
return static::$instance;
}
protected function __construct() {
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbname";
$this->_dbh_conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$this->_dbh_conn->exec("set names utf8");
$this->_dbh_conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->_dbh_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public function &dbh_conn() {
return $this->_dbh_conn;
}
private function __clone() { }
private function __wakeup() { }
}
And when you include this file, you need to get the connection like this:
当您包含此文件时,您需要获得如下连接:
$dbh_conn = DB::getInstance()->dbh_conn();
Now you have the connection to the database in $dbh_conn
and you can use it just as you did up until now. The only difference is that now you can include this file, but it won't connect to the DB unless you use the Singleton class DB
and specifically requesting the DB connection (as I did in the above example). Additional benefit is that you're getting the same DB connection throughout the runtime of the script because you're using a design pattern of a singleton class to get the connection.
现在,您可以在$ dbh_conn中连接到数据库,并且可以像直到现在一样使用它。唯一的区别是现在你可以包含这个文件,但它不会连接到数据库,除非你使用Singleton类DB并特别请求数据库连接(就像我在上面的例子中所做的那样)。另外一个好处是,您在整个脚本运行时获得相同的数据库连接,因为您正在使用单例类的设计模式来获取连接。