- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好的,我想我应该首先说明下面的代码片段是我的最终目标。我希望能够在我的应用程序中的任何位置运行这样的一行,并让它处理 PDO 连接和执行:
数据库::查询('QUERY');
为此,我在后台运行了以下两个文件。
Connection.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;
}
}
Database.php:是连接之间的中介。
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);
}
}
我的问题是:这是否有效?有更简单的方法吗?我将不胜感激任何指导。我喜欢将自己视为初学者,但我确实缺乏确保应用程序高效并减轻自身重量的经验。
最佳答案
这效率不高。每次执行查询时,都会创建一个新的 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;
}
...
关于php - 如何让我的 PDO 连接更有效率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19195330/
PHP查询: prepare('SELECT * FROM Locations WHERE user_id = :id LIMIT 0, 5'); $query->bindParam(":id
到目前为止我一直在使用 PDO->bindParam 但是在阅读手册时我发现 PDO->bindValue 我可以告诉 PDO->bindValue 按值传递,而 PDO->bindParam 按引用
我使用PDO进行MySQL数据库连接、选择、更新和删除。 但是我在选择带有特殊字符的行时遇到问题,例如,我想选择带有“Judge-Fürstová Mila”的页面标题, 表页, id titl
我使用的是 PHP 5.2.9 和 Apache 2.2.11 以及 mysql 5.1.32 为什么我不能禁用 PDO::ATTR_EMULATE_PREPARES ? 下面是代码: false)
当 PDO::ATTR_PERSISTENT = true 时,PDO::exec 和 PDO::query 之间似乎存在差异。当使用PDO::exec时,连接将不会被重用,并最终导致MySQL出现“
我正在尝试调整 Zend Skeleton App 以使用 ODBC 连接。我能够在 Zend 之外设置 PDO 连接(同一个表、服务器、所有内容),如下所示: new PDO('odbc:DRIVE
我正在使用 XAMPP 3.2.1 在 Windows 7 上使用 PDO,但在使其正常工作时遇到问题,即使它可以在我的共享托管服务器。 settings.php db.php setAttribu
我正在尝试使用以下代码从 get 变量中获取结果,我使用了另一个代码(在下面列出)并且它有效但是在使用它时无法逃脱,我不知道我有什么做错了,但我需要帮助,我刚刚开始 PDO,所以是的,我是个白痴 :D
我有这个代码 try { $dbh = new PDO('mysql:host=localhost;dbname=db_informations', 'root', ''); $dbh
在我的本地开发机器 (MAMP) 上,我的 MySQL 查询返回正确的大小写。但是,在我的共享托管服务器上,使用 fetch(PDO::FETCH_ASSOC); 时,我的结果是小写的 我试过以下方法
我正在使用这个 PDO 连接: try{ $db=new PDO(" mysql:host=localhost; dbname=...", "dbu...", "pass", array(PDO::M
我需要将 PDO 连接从 controller 传递到 cart 类, function __construct($connection) { $this->cart = new cart($
位于 PHP.net 的 PDO 交易示例表明 PDO::exec() 是事务处理的,但是没有这样的例子使用 PDO::query()。 事务是否涵盖 PDO::query()? 此外,据我所知,PD
几天来我一直在使用来自 http://www.mamp.info 的 MAMP Pro 3.07 . 很棒的工具,一切正常。 但是现在我遇到了问题,对于一个新项目,我需要连接到 MSSQL。 我在 w
我正在尝试在我的 PHP 代码中使用 PDO 模块来连接到数据库。我已经阅读并搜索了类似的主题,但我无法弄清楚我做错了什么。请帮我解决问题。 Apache版本:Apache/2.2.21 (Win32
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我使用 PDO 连接到我的数据库,我不知道哪种方法比更新、删除和插入、PDO::exec 或 PDO::excute 的另一种方法更好。我应该使用哪个? 最佳答案 虽然这两种方法具有相似的命名(exe
如何在 Codeigniter 中设置 PDO 数据库句柄的属性(PDO::ATTR_ERRMODE)? 最佳答案 我认为更好的选择是使用 MY_Model(然后您对其进行扩展,然后它在整个应用程序中
从 mysql 切换到 PDO 我注意到 fetch 方法改变了 PDO 对象本身!例如: $res=$ps->fetchAll(); echo($res[0]['Mail']); //it'ok /
嗨,我正在使用 PDO 为 mysql 开发一个基本的数据库连接类,但是当我 var dump 返回结果时,它告诉我我有 2 个对象:object(PDO)[2]。不应该只有一个吗? 这是迄今为止我的
我是一名优秀的程序员,十分优秀!