【问题标题】:how i can prevent multiple time connected to database我如何防止多次连接到数据库
【发布时间】: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


【解决方案1】:

不要从数据库类扩展您的应用程序类。

这是您的主要问题。
它们是完全不同的类,没有共同点。
所以你只需要使用你的 db 对象作为 property

Connection 类目前也没什么用,因为它只是 PDO 的伪装。

所以,只需创建原始 PDO 对象,然后将其传递到产品的构造函数中

class ProductInsert
{
    function __construct(PDO $db)
    {  
        $this->db = $db;  
    }    
    public function prdInsert(.....)  
}

看在上帝的份上,学会缩进你的代码。

【讨论】:

  • 谢谢,但直到现在我也收到通知:undefined var db 你能详细说明一下吗
  • 您必须创建原始 PDO 对象,然后将其传递给产品的构造函数$procuct = new ProductInsert($pdo);
  • 既然您在谈论is-a 关系,您可能需要为他注意Liskov 替换原则。但是 +1
  • @DaveJust 它是如何适用的,如果产品绝不是 DB 的子类型?
  • 不,我不是指当前示例,我指的是您关于is-a/has-a 关系的观点,它是LSP 的一部分,显然是正确的。我只是说你所说的东西有一个名字:) (LSP)
【解决方案2】:
 class Connection {  
        public function __construct($db) {  
        return $this->connect($db);  
        }  
        private function connect($db) {  
        try {  
        if (! $db){  
        $db = new PDO ( 'mysql:host=localhost;dbname=Shopping;charset=utf8', 'xxxx', 'xxxxx' );  
        echo 'new connection';  
        }
        return $db;  
        }  
        catch ( PDOException $conError ) {  
        echo 'failed to connect DB' . $conError->getMessage ();  
        }    

        }
}

【讨论】:

  • 谢谢。我试过这个它显示注意:未定义的属性:ProductInsert::$db
  • $db 变量是什么?你能把它从你的代码中删除吗?
  • $db 变量是一个 PDO 对象,没有这个(PDO 对象)我怎么能前进(比如在 db 中写入查询)?
  • 引用 db 属性 ($something->db) 怎么样?能不能多给点代码,展示起来会更方便吗?
  • 其实我的 $db = new pdo(....) ,这个 $db 对象用于全局,它只在不同的文件中调用一次我怎么能这样做
【解决方案3】:

这里是解决方案
pdo.php

class Connection {
// Declare instance  
private static $instance = NULL;  
// the constructor is set to private so so nobody can create a new instance using new
private function __construct() {
// maybe set the db name here later 
}
// Return DB instance or create intitial connection @return object (PDO) @access public
public static function getInstance() {
    if (! self::$instance) {
        self::$instance = new PDO ( 'mysql:host=localhost;dbname=Shopping;charset=utf8', 'root', 'vss0ftech' );
        self::$instance->setAttribute ( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        echo 'connected';
    } else
        echo 'connection is already existed';
    return self::$instance;     
}
// Like the constructor, we make __clone private so nobody can clone the instance
PRIVATE FUNCTION __CLONE() {
}
}

product.php

这里我们可以这样调用方法

class ProductInsert {  
    public function prdInsert(.....) {  
        $result_By_Vendor_And_Title = Connection::getInstance ()->query ( "select * from........)  
     }  
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 2013-12-26
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    相关资源
    最近更新 更多