Php 如何提高PDO连接的效率?
好的,我想我应该首先说下面的片段是我的最终目标。我希望能够在我的应用程序中的任何地方运行这样的行,并让它处理PDO连接和执行:Php 如何提高PDO连接的效率?,php,mysql,pdo,Php,Mysql,Pdo,好的,我想我应该首先说下面的片段是我的最终目标。我希望能够在我的应用程序中的任何地方运行这样的行,并让它处理PDO连接和执行: Database::query('query') 为此,我在后台运行了以下两个文件 php:处理PDO连接 class Connection { /** * The PDO connection. * * @var PDO */ protected $pdo = NULL; /** * The
Database::query('query')代码>
为此,我在后台运行了以下两个文件
php:处理PDO连接
class Connection {
/**
* The PDO connection.
*
* @var PDO
*/
protected $pdo = NULL;
/**
* The type of database we're connection to.
*
* @var string
*/
protected $type = '';
/**
* The name of the connected database.
*
* @var string
*/
protected $database = '';
/**
* The database connection details.
*
* @var array
*/
protected $config = array();
/**
* Create the connection instance.
*
*/
public function __construct()
{
// Import the configuration information from database.php
$this->config = Config::load('database');
// Pull the database type
$this->type = $this->config['database'];
// Pull the database name
if(isset($this->config[$this->type]['database']))
{
$this->database = $this->config[$this->type]['database'];
}
// Check to see if a connection has been made or not
if($this->pdo==NULL)
{
// Create the connection
$this->pdo = $this->createConnection();
}
}
/**
* Query the database.
*
* @param string $query
* @return array
*/
public function query($query)
{
// Check to see if we have a connection
if($this->pdo!=NULL)
{
// Execute the raw query
$query = $this->pdo->query($query);
return $query;
}
return false;
}
/**
* Execute a query on the database.
*
* @param string $query
* @return int
*/
public function exec($query)
{
// Check to see if we have a connection
if($this->pdo!=NULL)
{
// Execute the raw query
$execution = $this->pdo->exec($query);
return $execution;
}
return false;
}
/**
* Execute a query and return the last inserted Id
*
* @param string $query
* @return int
*/
public function execLastId($query)
{
// Check to see if we have a connection
if($this->pdo!=NULL)
{
// Execute the query and return the Id
if($this->exec($query))
{
return $this->pdo->lastInsertId();
}
}
return false;
}
/**
* Prepare and execute against the database.
*
* @param string $query
* @param array $params
* @return array
*/
public function prepare($query, $params)
{
// Check to see if we have a connection
if($this->pdo!=NULL)
{
// Prepare the query
$query = $this->pdo->prepare($query);
// Execute the query
$query->execute($params);
return $query->fetchAll();
}
return false;
}
/**
* Create a new PDO connection.
*
* @return PDO
*/
protected function createConnection()
{
// See if we can attempt to make a connection
if(isset($this->config[$this->type]))
{
$hasDSN = false;
// Decide what DSN to use
if($this->type=='mysql')
{
$hasDSN = true;
$dsn = $this->getMySQLDSN();
}
// If a DSN has been found, make the connection
if($hasDSN)
{
$username = $this->config[$this->type]['username'];
$password = $this->config[$this->type]['password'];
return new PDO($dsn, $username, $password);
}
}
return NULL;
}
/**
* Get the MySQL DSN.
*
* @return string
*/
protected function getMySQLDSN()
{
return 'mysql:host='.$this->config['mysql']['hostname'].';dbname='.$this->database;
}
}
class Database {
/**
* Run a raw query on the database.
*
* @param string $query
* @return array
*/
public static function query($query)
{
// Create the connection
$conn = new Connection;
// Return the query
return $conn->query($query);
}
/**
* Execute a query on the database.
*
* @param string $query
* @return int
*/
public static function exec($query)
{
// Create the connection
$conn = new Connection;
// Return the query
return $conn->exec($query);
}
/**
* Execute a query and return the last inserted Id
*
* @param string $query
* @return int
*/
public static function execLastId($query)
{
// Create the connection
$conn = new Connection;
// Return the query
return $conn->execLastId($query);
}
/**
* Prepare and then execute a query.
*
* @param string $query
* @param array $params
* @return array
*/
public static function prepare($query, array $params)
{
// Create the connection
$conn = new Connection;
// Return the query
return $conn->prepare($query, $params);
}
}
php:是连接之间的中介
class Connection {
/**
* The PDO connection.
*
* @var PDO
*/
protected $pdo = NULL;
/**
* The type of database we're connection to.
*
* @var string
*/
protected $type = '';
/**
* The name of the connected database.
*
* @var string
*/
protected $database = '';
/**
* The database connection details.
*
* @var array
*/
protected $config = array();
/**
* Create the connection instance.
*
*/
public function __construct()
{
// Import the configuration information from database.php
$this->config = Config::load('database');
// Pull the database type
$this->type = $this->config['database'];
// Pull the database name
if(isset($this->config[$this->type]['database']))
{
$this->database = $this->config[$this->type]['database'];
}
// Check to see if a connection has been made or not
if($this->pdo==NULL)
{
// Create the connection
$this->pdo = $this->createConnection();
}
}
/**
* Query the database.
*
* @param string $query
* @return array
*/
public function query($query)
{
// Check to see if we have a connection
if($this->pdo!=NULL)
{
// Execute the raw query
$query = $this->pdo->query($query);
return $query;
}
return false;
}
/**
* Execute a query on the database.
*
* @param string $query
* @return int
*/
public function exec($query)
{
// Check to see if we have a connection
if($this->pdo!=NULL)
{
// Execute the raw query
$execution = $this->pdo->exec($query);
return $execution;
}
return false;
}
/**
* Execute a query and return the last inserted Id
*
* @param string $query
* @return int
*/
public function execLastId($query)
{
// Check to see if we have a connection
if($this->pdo!=NULL)
{
// Execute the query and return the Id
if($this->exec($query))
{
return $this->pdo->lastInsertId();
}
}
return false;
}
/**
* Prepare and execute against the database.
*
* @param string $query
* @param array $params
* @return array
*/
public function prepare($query, $params)
{
// Check to see if we have a connection
if($this->pdo!=NULL)
{
// Prepare the query
$query = $this->pdo->prepare($query);
// Execute the query
$query->execute($params);
return $query->fetchAll();
}
return false;
}
/**
* Create a new PDO connection.
*
* @return PDO
*/
protected function createConnection()
{
// See if we can attempt to make a connection
if(isset($this->config[$this->type]))
{
$hasDSN = false;
// Decide what DSN to use
if($this->type=='mysql')
{
$hasDSN = true;
$dsn = $this->getMySQLDSN();
}
// If a DSN has been found, make the connection
if($hasDSN)
{
$username = $this->config[$this->type]['username'];
$password = $this->config[$this->type]['password'];
return new PDO($dsn, $username, $password);
}
}
return NULL;
}
/**
* Get the MySQL DSN.
*
* @return string
*/
protected function getMySQLDSN()
{
return 'mysql:host='.$this->config['mysql']['hostname'].';dbname='.$this->database;
}
}
class Database {
/**
* Run a raw query on the database.
*
* @param string $query
* @return array
*/
public static function query($query)
{
// Create the connection
$conn = new Connection;
// Return the query
return $conn->query($query);
}
/**
* Execute a query on the database.
*
* @param string $query
* @return int
*/
public static function exec($query)
{
// Create the connection
$conn = new Connection;
// Return the query
return $conn->exec($query);
}
/**
* Execute a query and return the last inserted Id
*
* @param string $query
* @return int
*/
public static function execLastId($query)
{
// Create the connection
$conn = new Connection;
// Return the query
return $conn->execLastId($query);
}
/**
* Prepare and then execute a query.
*
* @param string $query
* @param array $params
* @return array
*/
public static function prepare($query, array $params)
{
// Create the connection
$conn = new Connection;
// Return the query
return $conn->prepare($query, $params);
}
}
我的问题是:这是否有效?有没有更简单的方法?我希望得到任何指导。我喜欢把自己看作一个初学者,但我确实缺乏确保应用程序高效和减轻自身重量的经验 这是没有效率的。每次执行查询时,都会创建一个新的连接对象,从而创建一个新的PDO连接。连接到数据库会给您带来一些开销。实际上,你不需要每次都连接。您可以只连接一次,然后将该连接用于后续查询
因此,只需稍作更改,您就可以让您的数据库类在首次使用时创建连接:
class Database {
/**
* Reference to the connection
*/
private static $connection = null;
/**
* Run a raw query on the database.
*
* @param string $query
* @return array
*/
public static function query($query)
{
// Get the connection
$conn = self::getConnection();
// Return the query
return $conn->query($query);
}
public static function getConnection()
{
// Create the connection if needed.
if (self::$connection === null)
{
self::$connection = new Connection;
}
// Return new or existing instance.
return self::$connection;
}
...
这是没有效率的。每次执行查询时,都会创建一个新的连接对象,从而创建一个新的PDO连接。连接到数据库会给您带来一些开销。实际上,你不需要每次都连接。您可以只连接一次,然后将该连接用于后续查询
因此,只需稍作更改,您就可以让您的数据库类在首次使用时创建连接:
class Database {
/**
* Reference to the connection
*/
private static $connection = null;
/**
* Run a raw query on the database.
*
* @param string $query
* @return array
*/
public static function query($query)
{
// Get the connection
$conn = self::getConnection();
// Return the query
return $conn->query($query);
}
public static function getConnection()
{
// Create the connection if needed.
if (self::$connection === null)
{
self::$connection = new Connection;
}
// Return new or existing instance.
return self::$connection;
}
...
这也意味着您将使用$database->query('query');而不是数据库::query('query');不,没有。它还是静止的。我认为这本身不是最好的解决方案,但这完全是另一种讨论,所以我现在不想去那里。:)这真的很有趣。。谢谢你的快速回复。我现在就要试一试!伟大的工作GolezTrol!我在那里抛出了一些echo语句,试图了解发生了什么,它使用的是旧连接(或现有连接)。再次感谢你的帮助。。咖啡我请客。GolezTrol,我想知道你为什么不喜欢我静态实现数据库的方式。对我来说,当我写代码的时候,它只是简单得多,但是我知道可能会有一些影响。你能告诉我这些是什么吗?这也意味着你将使用$database->query('query');而不是数据库::query('query');不,没有。它还是静止的。我认为这本身不是最好的解决方案,但这完全是另一种讨论,所以我现在不想去那里。:)这真的很有趣。。谢谢你的快速回复。我现在就要试一试!伟大的工作GolezTrol!我在那里抛出了一些echo语句,试图了解发生了什么,它使用的是旧连接(或现有连接)。再次感谢你的帮助。。咖啡我请客。GolezTrol,我想知道你为什么不喜欢我静态实现数据库的方式。对我来说,当我写代码的时候,它只是简单得多,但是我知道可能会有一些影响。你能告诉我那些是什么吗?读