I've been playing around with PDO for the last few days, I'm working on a small CMS system to teach myself OOP skills, but even though it's only a small CMS, I want it to be able to handle whatever the web can throw at it.
最近几天我一直在玩PDO,我正在开发一个小型的CMS系统来自学OOP技能,但是尽管它只是一个小型的CMS,我还是希望它能够处理web可以处理的任何东西。
This is what I've come up with so far, I'm going to add connection pooling to the constructor to enable large amounts of concurrent connects on demand. I'm very new to this OOP stuff so I'm wanting a little advise and critism, no doubt I've done something terribly wrong here.
这就是我到目前为止得出的结论,我将向构造函数添加连接池,以便根据需要启用大量并发连接。我对这个OOP很陌生,所以我想要一些建议和批评,毫无疑问,我在这里做了一些非常糟糕的事情。
I took the top answer to Global or Singleton for database connection? as the base design, although I've added a private constructor as I want to use $this->dbConnectionInstance throughout the class for numerous helper functions to use.
我选择了全局或单例数据库连接的最上面的答案?作为基本设计,尽管我已经添加了一个私有构造函数,因为我想在整个类中使用$this->dbConnectionInstance作为许多要使用的助手函数。
Thanks very much for your time, I really will appreciate any advise you can give me,
非常感谢您的时间,我真的很感激您给我的建议,
-Drew
画了
// Usage Example: $dbconn = dbManager::getConnection();
// $dbconn->query("SELECT * FROM accounts WHERE id=:id", "':id' => $id");
<?php
class dbManager {
private static $dbManagerInstance;
private $dbConnectionInstance;
private $stime;
private $etime;
public $timespent;
public $numqueries;
public $queries = array();
public static function getManager(){
if (!self::$dbManagerInstance){
self::$dbManagerInstance = new dbManager();
}
return self::$dbManagerInstance;
}
// Server details stored in definition file
private function __construct($db_server=DB_SERVER, $db_user=DB_USER, $db_pass=DB_PASS, $db_params=array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")) {
if(!$this->dbConnectionInstance)
{
try{
$this->dbConnectionInstance = new PDO($db_server, $db_user, $db_pass, $db_params);
$this->dbConnectionInstance->setAttribute(PDO::ATTR_PERSISTENT, PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
$this->dbConnectionInstance = null;
die($e->getMessage());
}
}
return $this->dbConnectionInstance;
}
private function __destruct(){
$this->dbConnectionInstance = null;
}
private function query($sql, $params = array()) {
$this->queries[] = $sql;
$this->numqueries++;
$this->sTime = microtime();
$stmt = $this->dbConnectionInstance->prepare($sql);
$stmt->execute($params);
$this->eTime = microtime();
$this->timespent += round($this->eTime - $this->sTime, 4);
return $stmt;
}
}
?>
Thank you both for your suggestions, I've now added the rollback and commit into my exception handling, I'm just researching the use of buffered queries, I'm not entirely sure what ths will give me?
谢谢你们的建议,我现在添加回滚和提交到我的异常处理中,我正在研究缓冲查询的使用,我不完全确定这会给我什么?
2 个解决方案
#1
3
Looks good, I would add rollback functionality, along with the buffered query/errorInfo suggestions (If you're using a RDBMS that supports transactions):
看起来不错,我将添加回滚功能,以及缓冲查询/errorInfo建议(如果您使用支持事务的RDBMS):
try {
$this->dbConnectionInstance->beginTransaction();
$stmt = $this->dbConnectionInstance->prepare($sql);
$stmt->execute($params);
$this->dbConnectionInstance->commit();
}catch(PDOException $e){
$this->dbConnectionInstance->rollback();
}
commit(),beginTransaction()
EDIT: added links below for more info on buffered queries:
编辑:添加了以下链接,以获取更多关于缓冲查询的信息:
- mysql performance blog
- mysql性能的博客
- pdo mysql buffered query support
- pdo mysql缓冲查询支持
- stack overflow: pdo buffered query problem
- 栈溢出:pdo缓冲查询问题
#2
0
The code you have dosent look too bad. however if i could make a couple small changes (mainly error handling).
你所发送的代码看起来太糟糕了。但是如果我可以做一些小的改变(主要是错误处理)。
both the prepare and execute statements will return false on error. and you can access the error from $this->dbConnectionInstance->errorInfo()
in your example above.
准备语句和执行语句都将在错误时返回false。您可以从上面示例中的$this->dbConnectionInstance->errorInfo()访问错误。
also if you plan on using any large queries I suggest using a buffered query: PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true
如果您打算使用任何大型查询,我建议使用缓冲查询:PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true
looks like a good start. Good luck on your CMS.
看起来是个好的开始。祝你的CMS好运。
#1
3
Looks good, I would add rollback functionality, along with the buffered query/errorInfo suggestions (If you're using a RDBMS that supports transactions):
看起来不错,我将添加回滚功能,以及缓冲查询/errorInfo建议(如果您使用支持事务的RDBMS):
try {
$this->dbConnectionInstance->beginTransaction();
$stmt = $this->dbConnectionInstance->prepare($sql);
$stmt->execute($params);
$this->dbConnectionInstance->commit();
}catch(PDOException $e){
$this->dbConnectionInstance->rollback();
}
commit(),beginTransaction()
EDIT: added links below for more info on buffered queries:
编辑:添加了以下链接,以获取更多关于缓冲查询的信息:
- mysql performance blog
- mysql性能的博客
- pdo mysql buffered query support
- pdo mysql缓冲查询支持
- stack overflow: pdo buffered query problem
- 栈溢出:pdo缓冲查询问题
#2
0
The code you have dosent look too bad. however if i could make a couple small changes (mainly error handling).
你所发送的代码看起来太糟糕了。但是如果我可以做一些小的改变(主要是错误处理)。
both the prepare and execute statements will return false on error. and you can access the error from $this->dbConnectionInstance->errorInfo()
in your example above.
准备语句和执行语句都将在错误时返回false。您可以从上面示例中的$this->dbConnectionInstance->errorInfo()访问错误。
also if you plan on using any large queries I suggest using a buffered query: PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true
如果您打算使用任何大型查询,我建议使用缓冲查询:PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true
looks like a good start. Good luck on your CMS.
看起来是个好的开始。祝你的CMS好运。