您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP单例模式编写的PDO类的程序

2025/3/13 12:30:09发布38次查看
下面的代码是用此前一个名为mypdo的类改写的,引入了单例模式来保证在全局调用中不会重复实例化这个类,降低系统资源的浪费,用php大部分操作都是和各种数据库打交道,包括mysql,redis,memcache等各种关系型和非关系型数据库,所以一个应用中会存在大量连接数据库的操作,如果不用单例模式,那每次都要new操作,但是每次new都会消耗大量的内存资源和系统资源,而且每次打开和关闭数据库连接都是对数据库的一种极大考验和浪费,代码如下:
dsn = 'mysql:host=' . $dbhost . ';dbname=' . $dbname; $this->dbh = new pdo($this->dsn, $dbuser, $dbpasswd); $this->dbh->exec('set character_set_connection=' . $dbcharset . ', character_set_results=' . $dbcharset . ', character_set_client=binary'); } catch(pdoexception $e) { $this->outputerror($e->getmessage()); } } /** * 防止克隆 * */ private function __clone() { } /** * singleton instance * * @return object */ public static function getinstance($dbhost, $dbuser, $dbpasswd, $dbname, $dbcharset) { if (self::$_instance === null) { self::$_instance = new self($dbhost, $dbuser, $dbpasswd, $dbname, $dbcharset); } return self::$_instance; } /** * query 查询 * * @param string $strsql sql语句 * @param string $querymode 查询方式(all or row) * @param boolean $debug * @return array */ public function query($strsql, $querymode = 'all', $debug = false) { if ($debug === true) $this->debug($strsql); $recordset = $this->dbh->query($strsql); $this->getpdoerror(); if ($recordset) { $recordset->setfetchmode(pdo::fetch_assoc); if ($querymode == 'all') { $result = $recordset->fetchall(); } elseif ($querymode == 'row') { $result = $recordset->fetch(); } } else { $result = null; } return $result; } /** * update 更新 * * @param string $table 表名 * @param array $arraydatavalue 字段与值 * @param string $where 条件 * @param boolean $debug * @return int */ public function update($table, $arraydatavalue, $where = '', $debug = false) { $this->checkfields($table, $arraydatavalue); if ($where) { $strsql = ''; foreach ($arraydatavalue as $key => $value) { $strsql.= , `$key`='$value'; } $strsql = substr($strsql, 1); $strsql = update `$table` set $strsql where $where; } else { $strsql = replace into `$table` (` . implode('`,`', array_keys($arraydatavalue)) . `) values (' . implode(',', $arraydatavalue) . '); } if ($debug === true) $this->debug($strsql); $result = $this->dbh->exec($strsql); $this->getpdoerror(); return $result; } /** * insert 插入 * * @param string $table 表名 * @param array $arraydatavalue 字段与值 * @param boolean $debug * @return int */ public function insert($table, $arraydatavalue, $debug = false) { $this->checkfields($table, $arraydatavalue); $strsql = insert into `$table` (` . implode('`,`', array_keys($arraydatavalue)) . `) values (' . implode(',', $arraydatavalue) . '); if ($debug === true) $this->debug($strsql); $result = $this->dbh->exec($strsql); $this->getpdoerror(); return $result; } /** * replace 覆盖方式插入 * * @param string $table 表名 * @param array $arraydatavalue 字段与值 * @param boolean $debug * @return int */ public function replace($table, $arraydatavalue, $debug = false) { $this->checkfields($table, $arraydatavalue); $strsql = replace into `$table`(` . implode('`,`', array_keys($arraydatavalue)) . `) values (' . implode(',', $arraydatavalue) . '); if ($debug === true) $this->debug($strsql); $result = $this->dbh->exec($strsql); $this->getpdoerror(); return $result; } /** * delete 删除 * * @param string $table 表名 * @param string $where 条件 * @param boolean $debug * @return int */ public function delete($table, $where = '', $debug = false) { if ($where == '') { $this->outputerror('where' is null); } else { $strsql = delete from `$table` where $where; if ($debug === true) $this->debug($strsql); $result = $this->dbh->exec($strsql); $this->getpdoerror(); return $result; } } /** * execsql 执行sql语句 * * @param string $strsql * @param boolean $debug * @return int */ public function execsql($strsql, $debug = false) { if ($debug === true) $this->debug($strsql); $result = $this->dbh->exec($strsql); $this->getpdoerror(); return $result; } /** * 获取字段最大值 * * @param string $table 表名 * @param string $field_name 字段名 * @param string $where 条件 */ public function getmaxvalue($table, $field_name, $where = '', $debug = false) { $strsql = select max( . $field_name . ) as max_value from $table; if ($where != '') $strsql.= where $where; if ($debug === true) $this->debug($strsql); $arrtemp = $this->query($strsql, 'row'); $maxvalue = $arrtemp[max_value]; if ($maxvalue == || $maxvalue == null) { $maxvalue = 0; } return $maxvalue; } /** * 获取指定列的数量 * * @param string $table * @param string $field_name * @param string $where * @param bool $debug * @return int */ public function getcount($table, $field_name, $where = '', $debug = false) { $strsql = select count($field_name) as num from $table; if ($where != '') $strsql.= where $where; if ($debug === true) $this->debug($strsql); $arrtemp = $this->query($strsql, 'row'); return $arrtemp['num']; } /** * 获取表引擎 * * @param string $dbname 库名 * @param string $tablename 表名 * @param boolean $debug * @return string */ public function gettableengine($dbname, $tablename) { $strsql = show table status from $dbname where name=' . $tablename . '; $arraytableinfo = $this->query($strsql); $this->getpdoerror(); return $arraytableinfo[0]['engine']; } /** * begintransaction 事务开始 */ private function begintransaction() { $this->dbh->begintransaction(); } /** * commit 事务提交 */ private function commit() { $this->dbh->commit(); } /** * rollback 事务回滚 */ private function rollback() { $this->dbh->rollback(); } /** * transaction 通过事务处理多条sql语句 * 调用前需通过gettableengine判断表引擎是否支持事务 * * @param array $arraysql * @return boolean */ public function exectransaction($arraysql) { $retval = 1; $this->begintransaction(); foreach ($arraysql as $strsql) { if ($this->execsql($strsql) == 0) $retval = 0; } if ($retval == 0) { $this->rollback(); return false; } else { $this->commit(); return true; } } /** * checkfields 检查指定字段是否在指定数据表中存在 * * @param string $table * @param array $arrayfield */ private function checkfields($table, $arrayfields) { $fields = $this->getfields($table); foreach ($arrayfields as $key => $value) { if (!in_array($key, $fields)) { $this->outputerror(unknown column `$key` in field list.); } } } /** * getfields 获取指定数据表中的全部字段名 * * @param string $table 表名 * @return array */ private function getfields($table) { $fields = array(); $recordset = $this->dbh->query(show columns from $table); $this->getpdoerror(); $recordset->setfetchmode(pdo::fetch_assoc); $result = $recordset->fetchall(); foreach ($result as $rows) { $fields[] = $rows['field']; } return $fields; } /** * getpdoerror 捕获pdo错误信息 */ private function getpdoerror() { if ($this->dbh->errorcode() != '00000') { $arrayerror = $this->dbh->errorinfo(); $this->outputerror($arrayerror[2]); } } /** * debug * * @param mixed $debuginfo */ private function debug($debuginfo) { var_dump($debuginfo); exit(); } /** * 输出错误信息 * * @param string $strerrmsg */ private function outputerror($strerrmsg) { throw new exception('mysql error: ' . $strerrmsg); } /** * destruct 关闭数据库连接 */ public function destruct() { $this->dbh = null; }}
调用方法:
destruct();
文章网址:
随意转载^^但请附上教程地址。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product