【发布时间】:2013-10-10 03:57:34
【问题描述】:
这是我的代码
pdo.php
class Connection {
public function __construct() {
$this->connect ($db);
}
private function connect($db) {
try {
if (! $db){
$this->db = new PDO ( 'mysql:host=localhost;dbname=Shopping;charset=utf8', 'xxxx', 'xxxxx' );
echo 'Connected';
return $this->db;
}
catch ( PDOException $conError ) {
echo 'failed to connect DB' . $conError->getMessage ();
}
}
}
product.php
class ProductInsert extends Connection { //here my function is called multiple times
function __construct() {
parent::__construct($db);
}
public function prdInsert(.....)
{ ........}
我的问题是多次打开数据库连接。每当我调用 productInsert() 时,就会打开数据库连接,如何防止这种情况发生
【问题讨论】:
-
使用后需要关闭数据库连接。
-
谢谢,但我认为这会对数据库造成负担,因为我的程序多次调用 productInsert()
-
为什么要扩展连接类来编写函数? db 连接是在每次调用的构造中建立的
-
@wordpresser 没有扩展连接我如何在我的函数中访问我的 $db 变量
标签: php mysql database pdo global-variables