也许这是一个愚蠢的问题,但我对PDO很新,很困惑。是否可以在一个函数中实例化一个PDO对象(打开与服务器/ db的连接),然后在另一个函数中关闭相同的连接?函数是否需要传递给对象以便能够关闭它?我想这样做,所以我可以创建一个无处不在的站点范围的函数,我可以调用它来启动连接,执行非泛型的SQL,然后用另一个站点范围的函数关闭它。我怎样才能做到这一点?我是否需要将对象作为参数传递给这些函数?
答案 0 :(得分:12)
是的,这是可能的。我建议使用一个启动MySQL连接的构造函数和一个使它无效的析构函数。这样,每次要打开和关闭连接时,都不必手动调用该函数。每当调用该对象的新实例时,该连接将打开,并且当没有对该对象的进一步引用时,该连接将被取消。
它可能看起来像这样:
private $l; //MySQL Connection
//Called automatically upon initiation
function __construct() {
try {
$this->l = new PDO("mysql:host=".MYSQL_HOST.";dbname=".MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD); //Initiates connection
$this->l->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); // Sets error mode
} catch (PDOException $e) {
file_put_contents("log/dberror.log", "Date: " . date('M j Y - G:i:s') . " ---- Error: " . $e->getMessage().PHP_EOL, FILE_APPEND);
die($e->getMessage()); // Log and display error in the event that there is an issue connecting
}
}
//Called automatically when there are no further references to object
function __destruct() {
try {
$this->l = null; //Closes connection
} catch (PDOException $e) {
file_put_contents("log/dberror.log", "Date: " . date('M j Y - G:i:s') . " ---- Error: " . $e->getMessage().PHP_EOL, FILE_APPEND);
die($e->getMessage());
}
}
您可能会发现有关构造函数和析构函数的引用很有用:http://php.net/manual/en/language.oop5.decon.php
答案 1 :(得分:2)
根据我的理解,您只需要连接一次数据库,而不必为每个功能打开连接。数据库连接将自动关闭,但您可以通过将空值分配给PDO对象来手动关闭它。
实例化PDO对象时进行连接。
$dbh = new PDO("mysql:host=$hostname;dbname=mysql", $username, $password);
并在脚本结束时自动关闭或者为其指定空值
$dbh = null;
有用的指南here
答案 2 :(得分:1)
的index.php:
class Xcrud
{ protected static $firephp;
protected static $_instance = array();
...
...
$db = Xcrud_db::get_instance($this->connection);
$db->query("SELECT `{$field}` FROM `{$this->table}` WHERE `{$this->primary}` = " . $db->escape($this->primary_key) . " LIMIT 1");
xcrud_db.php:
class Xcrud_db
{ protected static $firephp;
private static $_instance = array();
private $connect;
private $result;
private $dbhost;
private $dbuser;
private $dbpass;
private $dbname;
private $dbencoding;
public static function get_instance($params = false)
{ some code
}
private function __construct($dbuser, $dbpass, $dbname, $dbhost, $dbencoding)
{
$this->firephp = FirePHP::getInstance(true);
$this->firephp->setEnabled(true);
//$this->firephp->log('xcrud_db.php:__construct');
if (strpos($dbhost, ':') !== false)
{
list($host, $port) = explode(':', $dbhost, 2);
$this->connect = mysqli_connect($host, $dbuser, $dbpass, $dbname, $port);
} else
$this->connect = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if (!$this->connect)
$this->error('Connection error. Can not connect to database');
$this->connect->set_charset(str_replace('-', '', $dbencoding));
if ($this->connect->error)
$this->error($this->connect->error);
}
public function query($query = '')
{
$this->firephp->log('xcrud_db.php:query='.$query);
$this->result = $this->connect->query($query); //echo $query;
if ($this->connect->error)
$this->error($this->connect->error);
return $this->connect->affected_rows;
}
etc