1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2017/7/24
  6. * Time: 14:03
  7. */
  8. /**
  9.  * 数据库PDO操作
  10.  */
  11. class MysqlPdo {
  12.  
  13. public static $PDOStatement = null;
  14.  
  15. /**
  16. * 数据库的连接参数配置
  17. * @var array
  18. * @access public
  19. */
  20. public static $config = array();
  21.  
  22. /**
  23. * 是否使用永久连接
  24. * @var bool
  25. * @access public
  26. */
  27. public static $pconnect = false;
  28.  
  29. /**
  30. * 错误信息
  31. * @var string
  32. * @access public
  33. */
  34. public static $error = '';
  35.  
  36. /**
  37. * 单件模式,保存Pdo类唯一实例,数据库的连接资源
  38. * @var object
  39. * @access public
  40. */
  41. protected static $link;
  42.  
  43. /**
  44. * 是否已经连接数据库
  45. * @var bool
  46. * @access public
  47. */
  48. public static $connected = false;
  49.  
  50. /**
  51. * 数据库版本
  52. * @var string
  53. * @access public
  54. */
  55. public static $dbVersion = null;
  56.  
  57. /**
  58. * 当前SQL语句
  59. * @var string
  60. * @access public
  61. */
  62. public static $queryStr = '';
  63.  
  64. /**
  65. * 最后插入记录的ID
  66. * @var integer
  67. * @access public
  68. */
  69. public static $lastInsertId = null;
  70.  
  71. /**
  72. * 返回影响记录数
  73. * @var integer
  74. * @access public
  75. */
  76. public static $numRows = 0;
  77.  
  78. // 事务指令数
  79. public static $transTimes = 0;
  80.  
  81. /**
  82. * 构造函数,
  83. * @param $dbconfig 数据库连接相关信息,array('ServerName', 'UserName', 'Password', 'DefaultDb', 'DB_Port', 'DB_TYPE')
  84. */
  85. public function __construct($dbConfig=''){
  86. if (!class_exists('PDO')) self::throw_exception("不支持:PDO");
  87.  
  88. //若没有传输任何参数,则使用默认的数据定义
  89. if (!is_array($dbConfig)) {
  90. $dbConfig = array(
  91. 'hostname' => DB_HOST,
  92. 'username' => DB_USER,
  93. 'password' => DB_PWD,
  94. 'database' => DB_NAME,
  95. 'hostport' => DB_PORT,
  96. 'dbms' => DB_TYPE,
  97. 'dsn' => DB_TYPE.":host=".DB_HOST.";dbname=".DB_NAME
  98. );
  99. }
  100.  
  101. if(empty($dbConfig['hostname'])) self::throw_exception("没有定义数据库配置");
  102. self::$config = $dbConfig;
  103. if(empty(self::$config['params'])) self::$config['params'] = array();
  104.  
  105. /*************************************华丽分隔线*******************************************/
  106.  
  107. if (!isset(self::$link) ) {
  108. $configs = self::$config;
  109. if(self::$pconnect) {
  110. $configs['params'][constant('PDO::ATTR_PERSISTENT')] = true;
  111. }
  112. try {
  113. self::$link = new PDO( $configs['dsn'], $configs['username'], $configs['password'],$configs['params']);
  114. } catch (PDOException $e) {
  115. self::throw_exception($e->getMessage());
  116. }
  117. if(!self::$link) {
  118. self::throw_exception('PDO CONNECT ERROR');
  119. return false;
  120. }
  121. self::$link->exec('SET NAMES '.DB_CHARSET);
  122. self::$dbVersion = self::$link->getAttribute(constant("PDO::ATTR_SERVER_INFO"));
  123.  
  124. // 标记连接成功
  125. self::$connected = true;
  126.  
  127. // 注销数据库连接配置信息
  128. unset($configs);
  129. }
  130. return self::$link;
  131. }
  132.  
  133. /**
  134. * 释放查询结果
  135. * @access function
  136. */
  137. static function free() {
  138. self::$PDOStatement = null;
  139. }
  140.  
  141. /*********************************************************************************************************/
  142. /* 数据库操作 */
  143. /*********************************************************************************************************/
  144. /**
  145. * 获得所有的查询数据
  146. * @access function
  147. * @return array
  148. */
  149. static function getAll($sql=null) {
  150. if($sql != null)
  151. {
  152. self::query($sql);
  153. }
  154. //返回数据集
  155. $result = self::$PDOStatement->fetchAll(constant('PDO::FETCH_ASSOC'));
  156. return $result;
  157. }
  158.  
  159. /**
  160. * 获得一条查询结果
  161. * @access function
  162. * @param string $sql SQL指令
  163. * @param integer $seek 指针位置
  164. * @return array
  165. */
  166. static function getRow($sql=null) {
  167. if($sql != null)
  168. {
  169. self::query($sql);
  170. }
  171. // 返回数组集
  172. $result = self::$PDOStatement->fetch(constant('PDO::FETCH_ASSOC'),constant('PDO::FETCH_ORI_NEXT'));
  173. return $result;
  174. }
  175.  
  176. /**
  177. * 执行sql语句,自动判断进行查询或者执行操作
  178. * @access function
  179. * @param string $sql SQL指令
  180. * @return mixed
  181. */
  182. static function doSql($sql='') {
  183. if(self::isMainIps($sql)) {
  184. return self::execute($sql);
  185. }else {
  186. return self::getAll($sql);
  187. }
  188. }
  189.  
  190. /**
  191. * 根据指定ID查找表中记录(仅用于单表操作)
  192. * @access function
  193. * @param integer $priId 主键ID
  194. * @param string $tables 数据表名
  195. * @param string $fields 字段名
  196. * @return ArrayObject 表记录
  197. */
  198. static function findById($tabName,$priId,$fields='*'){
  199. $sql = 'SELECT %s FROM %s WHERE id=%d';
  200. return self::getRow(sprintf($sql, self::parseFields($fields), $tabName, $priId));
  201. }
  202.  
  203. /**
  204. * 查找记录
  205. * @access function
  206. * @param string $tables 数据表名
  207. * @param mixed $where 查询条件
  208. * @param string $fields 字段名
  209. * @param string $order 排序
  210. * @param string $limit 取多少条数据
  211. * @param string $group 分组
  212. * @param string $having
  213. * @param boolean $lock 是否加锁
  214. * @return ArrayObject
  215. */
  216. static function find($tables,$where="",$fields='*',$order=null,$limit=null,$group=null,$having=null) {
  217. $sql = 'SELECT '.self::parseFields($fields)
  218. .' FROM '.$tables
  219. .self::parseWhere($where)
  220. .self::parseGroup($group)
  221. .self::parseHaving($having)
  222. .self::parseOrder($order)
  223. .self::parseLimit($limit);
  224. $dataAll = self::getAll($sql);
  225. if(count($dataAll)==1){$rlt=$dataAll[0];}else{$rlt=$dataAll;}
  226. return $rlt;
  227. }
  228.  
  229. /**
  230. * 插入(单条)记录
  231. * @access function
  232. * @param mixed $data 数据
  233. * @param string $table 数据表名
  234. * @return false | integer
  235. */
  236. function add($data,$table) {
  237. //过滤提交数据
  238. $data=self::filterPost($table,$data);
  239. foreach ($data as $key=>$val){
  240. if(is_array($val) && strtolower($val[0]) == 'exp') {
  241. $val = $val[1]; // 使用表达式 ???
  242. }elseif (is_scalar($val)){
  243. $val = self::fieldFormat($val);
  244. }else{
  245. // 去掉复合对象
  246. continue;
  247. }
  248. $data[$key] = $val;
  249. }
  250. $fields = array_keys($data);
  251. array_walk($fields, array($this, 'addSpecialChar'));
  252. $fieldsStr = implode(',', $fields);
  253. $values = array_values($data);
  254. $valuesStr = implode(',', $values);
  255. $sql = 'INSERT INTO '.$table.' ('.$fieldsStr.') VALUES ('.$valuesStr.')';
  256. return self::execute($sql);
  257. }
  258.  
  259. /**
  260. * 更新记录
  261. * @access function
  262. * @param mixed $sets 数据
  263. * @param string $table 数据表名
  264. * @param string $where 更新条件
  265. * @param string $limit
  266. * @param string $order
  267. * @return false | integer
  268. */
  269. static function update($sets,$table,$where,$limit=0,$order='') {
  270. $sets = self::filterPost($table,$sets);
  271. $sql = 'UPDATE '.$table.' SET '.self::parseSets($sets).self::parseWhere($where).self::parseOrder($order).self::parseLimit($limit);
  272. return self::execute($sql);
  273. }
  274.  
  275. /**
  276. * 保存某个字段的值
  277. * @access function
  278. * @param string $field 要保存的字段名
  279. * @param string $value 字段值
  280. * @param string $table 数据表
  281. * @param string $where 保存条件
  282. * @param boolean $asString 字段值是否为字符串
  283. * @return void
  284. */
  285. static function setField($field, $value, $table, $condition="", $asString=false) {
  286. // 如果有'(' 视为 SQL指令更新 否则 更新字段内容为纯字符串
  287. if(false === strpos($value,'(') || $asString) $value = '"'.$value.'"';
  288. $sql = 'UPDATE '.$table.' SET '.$field.'='.$value.self::parseWhere($condition);
  289. return self::execute($sql);
  290. }
  291.  
  292. /**
  293. * 删除记录
  294. * @access function
  295. * @param mixed $where 为条件Map、Array或者String
  296. * @param string $table 数据表名
  297. * @param string $limit
  298. * @param string $order
  299. * @return false | integer
  300. */
  301. static function remove($where,$table,$limit='',$order='') {
  302. $sql = 'DELETE FROM '.$table.self::parseWhere($where).self::parseOrder($order).self::parseLimit($limit);
  303. return self::execute($sql);
  304. }
  305.  
  306. /**
  307. +----------------------------------------------------------
  308. * 修改或保存数据(仅用于单表操作)
  309. * 有主键ID则为修改,无主键ID则为增加
  310. * 修改记录:
  311. +----------------------------------------------------------
  312. * @access function
  313. +----------------------------------------------------------
  314. * @param $tabName 表名
  315. * @param $aPost 提交表单的 $_POST
  316. * @param $priId 主键ID
  317. * @param $aNot 要排除的一个字段或数组
  318. * @param $aCustom 自定义的一个数组,附加到数据库中保存
  319. * @param $isExits 是否已经存在 存在:true, 不存在:false
  320. +----------------------------------------------------------
  321. * @return Boolean 修改或保存是否成功
  322. +----------------------------------------------------------
  323. */
  324. function saveOrUpdate($tabName, $aPost, $priId="", $aNot="", $aCustom="", $isExits=false) {
  325. if(empty($tabName) || !is_array($aPost) || is_int($aNot)) return false;
  326. if(is_string($aNot) && !empty($aNot)) $aNot = array($aNot);
  327. if(is_array($aNot) && is_int(key($aNot))) $aPost = array_diff_key($aPost, array_flip($aNot));
  328. if(is_array($aCustom) && is_string(key($aCustom))) $aPost = array_merge($aPost,$aCustom);
  329. if (empty($priId) && !$isExits) { //新增
  330. $aPost = array_filter($aPost, array($this, 'removeEmpty'));
  331. return self::add($aPost, $tabName);
  332. } else { //修改
  333. return self::update($aPost, $tabName, "id=".$priId);
  334. }
  335. }
  336.  
  337. /**
  338. * 获取最近一次查询的sql语句
  339. * @access function
  340. * @param
  341. * @return String 执行的SQL
  342. */
  343. static function getLastSql() {
  344. $link = self::$link;
  345. if ( !$link ) return false;
  346. return self::$queryStr;
  347. }
  348.  
  349. /**
  350. * 获取最后插入的ID
  351. * @access function
  352. * @param
  353. * @return integer 最后插入时的数据ID
  354. */
  355. static function getLastInsId(){
  356. $link = self::$link;
  357. if ( !$link ) return false;
  358. return self::$lastInsertId;
  359. }
  360.  
  361. /**
  362. * 获取DB版本
  363. * @access function
  364. * @param
  365. * @return string
  366. */
  367. static function getDbVersion(){
  368. $link = self::$link;
  369. if ( !$link ) return false;
  370. return self::$dbVersion;
  371. }
  372.  
  373. /**
  374. * 取得数据库的表信息
  375. * @access function
  376. * @return array
  377. */
  378. static function getTables() {
  379. $info = array();
  380. if(self::query("SHOW TABLES")) {
  381. $result = self::getAll();
  382. foreach ($result as $key => $val) {
  383. $info[$key] = current($val);
  384. }
  385. }
  386. return $info;
  387. }
  388.  
  389. /**
  390. * 取得数据表的字段信息
  391. * @access function
  392. * @return array
  393. */
  394. static function getFields($tableName) {
  395. // 获取数据库联接
  396. $link = self::$link;
  397. $sql = "SELECT
  398. ORDINAL_POSITION ,COLUMN_NAME, COLUMN_TYPE, DATA_TYPE,
  399. IF(ISNULL(CHARACTER_MAXIMUM_LENGTH), (NUMERIC_PRECISION + NUMERIC_SCALE), CHARACTER_MAXIMUM_LENGTH) AS MAXCHAR,
  400. IS_NULLABLE, COLUMN_DEFAULT, COLUMN_KEY, EXTRA, COLUMN_COMMENT
  401. FROM
  402. INFORMATION_SCHEMA.COLUMNS
  403. WHERE
  404. TABLE_NAME = :tabName AND TABLE_SCHEMA='".DB_NAME."'";
  405. self::$queryStr = sprintf($sql, $tableName);
  406. $sth = $link->prepare($sql);
  407. $sth->bindParam(':tabName', $tableName);
  408. $sth->execute();
  409. $result = $sth->fetchAll(constant('PDO::FETCH_ASSOC'));
  410. $info = array();
  411. foreach ($result as $key => $val) {
  412. $info[$val['COLUMN_NAME']] = array(
  413. 'postion' => $val['ORDINAL_POSITION'],
  414. 'name' => $val['COLUMN_NAME'],
  415. 'type' => $val['COLUMN_TYPE'],
  416. 'd_type' => $val['DATA_TYPE'],
  417. 'length' => $val['MAXCHAR'],
  418. 'notnull' => (strtolower($val['IS_NULLABLE']) == "no"),
  419. 'default' => $val['COLUMN_DEFAULT'],
  420. 'primary' => (strtolower($val['COLUMN_KEY']) == 'pri'),
  421. 'autoInc' => (strtolower($val['EXTRA']) == 'auto_increment'),
  422. 'comment' => $val['COLUMN_COMMENT']
  423. );
  424. }
  425. // 有错误则抛出异常
  426. self::haveErrorThrowException();
  427. return $info;
  428. }
  429.  
  430. /**
  431. * 关闭数据库
  432. * @access function
  433. */
  434. static function close() {
  435. self::$link = null;
  436. }
  437.  
  438. /**
  439. * SQL指令安全过滤
  440. * @access function
  441. * @param string $str SQL指令
  442. * @return string
  443. */
  444. static function escape_string($str) {
  445. return addslashes($str);
  446. }
  447.  
  448. /*********************************************************************************************************/
  449. /* 内部操作方法 */
  450. /*********************************************************************************************************/
  451. /**
  452. * 有出错抛出异常
  453. * @access function
  454. * @return
  455. */
  456. static function haveErrorThrowException() {
  457. $obj = empty(self::$PDOStatement) ? self::$link : self::$PDOStatement;
  458. $arrError = $obj->errorInfo();
  459. if($arrError[0] !== '00000') { // 有错误信息
  460. self::$error = $arrError[0]."|".$arrError[2]. "<br/>[ SQL ] : ".self::$queryStr."<br/>";
  461. self::throw_exception(self::$error);
  462. return false;
  463. }
  464. //主要针对execute()方法抛出异常
  465. if(self::$queryStr=='')self::throw_exception('Query was empty<br/><br/>[ SQL语句 ] :');
  466. }
  467.  
  468. /**
  469. * where分析
  470. * @access function
  471. * @param mixed $where 查询条件
  472. * @return string
  473. */
  474. static function parseWhere($where) {
  475. $whereStr = '';
  476. if(is_string($where) || is_null($where)) {
  477. $whereStr = $where;
  478. }
  479. return empty($whereStr)?'':' WHERE '.$whereStr;
  480. }
  481.  
  482. /**
  483. * order分析
  484. * @access function
  485. * @param mixed $order 排序
  486. * @return string
  487. */
  488. static function parseOrder($order) {
  489. $orderStr = '';
  490. if(is_array($order))
  491. $orderStr .= ' ORDER BY '.implode(',', $order);
  492. else if(is_string($order) && !empty($order))
  493. $orderStr .= ' ORDER BY '.$order;
  494. return $orderStr;
  495. }
  496.  
  497. /**
  498. * limit分析
  499. * @access function
  500. * @param string $limit
  501. * @return string
  502. */
  503. static function parseLimit($limit) {
  504. $limitStr = '';
  505. if(is_array($limit)) {
  506. if(count($limit)>1)
  507. $limitStr .= ' LIMIT '.$limit[0].' , '.$limit[1].' ';
  508. else
  509. $limitStr .= ' LIMIT '.$limit[0].' ';
  510. } else if(is_string($limit) && !empty($limit)) {
  511. $limitStr .= ' LIMIT '.$limit.' ';
  512. }
  513. return $limitStr;
  514. }
  515.  
  516. /**
  517. * group分析
  518. * @access function
  519. * @param mixed $group
  520. * @return string
  521. */
  522. static function parseGroup($group) {
  523. $groupStr = '';
  524. if(is_array($group))
  525. $groupStr .= ' GROUP BY '.implode(',', $group);
  526. else if(is_string($group) && !empty($group))
  527. $groupStr .= ' GROUP BY '.$group;
  528. return empty($groupStr)?'':$groupStr;
  529. }
  530.  
  531. /**
  532. * having分析
  533. * @access function
  534. * @param string $having
  535. * @return string
  536. */
  537. static function parseHaving($having) {
  538. $havingStr = '';
  539. if(is_string($having) && !empty($having))
  540. $havingStr .= ' HAVING '.$having;
  541. return $havingStr;
  542. }
  543.  
  544. /**
  545. * fields分析
  546. * @access function
  547. * @param mixed $fields
  548. * @return string
  549. */
  550. function parseFields($fields) {
  551. if(is_array($fields)) {
  552. array_walk($fields, array($this, 'addSpecialChar'));
  553. $fieldsStr = implode(',', $fields);
  554. }else if(is_string($fields) && !empty($fields)) {
  555. if( false === strpos($fields,'`') ) {
  556. $fields = explode(',',$fields);
  557. array_walk($fields, array($this, 'addSpecialChar'));
  558. $fieldsStr = implode(',', $fields);
  559. }else {
  560. $fieldsStr = $fields;
  561. }
  562. }else $fieldsStr = '*';
  563. return $fieldsStr;
  564. }
  565.  
  566. /**
  567. * sets分析,在更新数据时调用
  568. * @access function
  569. * @param mixed $values
  570. * @return string
  571. */
  572. private function parseSets($sets) {
  573. $setsStr = '';
  574. if(is_array($sets)){
  575. foreach ($sets as $key=>$val){
  576. $key = self::addSpecialChar($key);
  577. $val = self::fieldFormat($val);
  578. $setsStr .= "$key = ".$val.",";
  579. }
  580. $setsStr = substr($setsStr,0,-1);
  581. }else if(is_string($sets)) {
  582. $setsStr = $sets;
  583. }
  584. return $setsStr;
  585. }
  586.  
  587. /**
  588. * 字段格式化
  589. * @access function
  590. * @param mixed $value
  591. * @return mixed
  592. */
  593. static function fieldFormat(&$value) {
  594. if(is_int($value)) {
  595. $value = intval($value);
  596. } else if(is_float($value)) {
  597. $value = floatval($value);
  598. } elseif(preg_match('/^\w∗(\+|\-|\*|\/)?\w∗$/i',$value)){
  599. // 支持在字段的值里面直接使用其它字段
  600. // 例如 (score+1) (name) 必须包含括号
  601. $value = $value;
  602. }else if(is_string($value)) {
  603. $value = '\''.self::escape_string($value).'\'';
  604. }
  605. return $value;
  606. }
  607.  
  608. /**
  609. * 字段和表名添加` 符合
  610. * 保证指令中使用关键字不出错 针对mysql
  611. * @access function
  612. * @param mixed $value
  613. * @return mixed
  614. */
  615. static function addSpecialChar(&$value) {
  616. if( '*' == $value || false !== strpos($value,'(') || false !== strpos($value,'.') || false !== strpos($value,'`')) {
  617. //如果包含* 或者 使用了sql方法 则不作处理
  618. } elseif(false === strpos($value,'`') ) {
  619. $value = '`'.trim($value).'`';
  620. }
  621. return $value;
  622. }
  623.  
  624. /**
  625. +----------------------------------------------------------
  626. * 去掉空元素
  627. +----------------------------------------------------------
  628. * @access function
  629. +----------------------------------------------------------
  630. * @param mixed $value
  631. +----------------------------------------------------------
  632. * @return mixed
  633. +----------------------------------------------------------
  634. */
  635. static function removeEmpty($value){
  636. return !empty($value);
  637. }
  638.  
  639. /**
  640. * 执行查询 主要针对 SELECT, SHOW 等指令
  641. * @access function
  642. * @param string $sql sql指令
  643. * @return mixed
  644. */
  645. static function query($sql='') {
  646. // 获取数据库联接
  647. $link = self::$link;
  648. if ( !$link ) return false;
  649. self::$queryStr = $sql;
  650. //释放前次的查询结果
  651. if ( !empty(self::$PDOStatement) ) self::free();
  652. self::$PDOStatement = $link->prepare(self::$queryStr);
  653. $bol = self::$PDOStatement->execute();
  654. // 有错误则抛出异常
  655. self::haveErrorThrowException();
  656. return $bol;
  657. }
  658.  
  659. /**
  660. * 数据库操作方法
  661. * @access function
  662. * @param string $sql 执行语句
  663. * @param boolean $lock 是否锁定(默认不锁定)
  664. * @return void
  665. public function execute($sql='',$lock=false) {
  666. if(empty($sql)) $sql = $this->queryStr;
  667. return $this->_execute($sql);
  668. }*/
  669. /**
  670. * 执行语句 针对 INSERT, UPDATE 以及DELETE
  671. * @access function
  672. * @param string $sql sql指令
  673. * @return integer
  674. */
  675. static function execute($sql='') {
  676. // 获取数据库联接
  677. $link = self::$link;
  678. if ( !$link ) return false;
  679. self::$queryStr = $sql;
  680. //释放前次的查询结果
  681. if ( !empty(self::$PDOStatement) ) self::free();
  682. $result = $link->exec(self::$queryStr);
  683. // 有错误则抛出异常
  684. self::haveErrorThrowException();
  685. if ( false === $result) {
  686. return false;
  687. } else {
  688. self::$numRows = $result;
  689. self::$lastInsertId = $link->lastInsertId();
  690. return self::$numRows;
  691. }
  692. }
  693. /**
  694. * 是否为数据库更改操作
  695. * @access private
  696. * @param string $query SQL指令
  697. * @return boolen 如果是查询操作返回false
  698. */
  699. static function isMainIps($query) {
  700. $queryIps = 'INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|SELECT .* INTO|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK';
  701. if (preg_match('/^\s*"?(' . $queryIps . ')\s+/i', $query)) {
  702. return true;
  703. }
  704. return false;
  705. }
  706.  
  707. /**
  708. * 过滤POST提交数据
  709. * @access private
  710. * @param mixed $data POST提交数据
  711. * @param string $table 数据表名
  712. * @return mixed $newdata
  713. */
  714. static function filterPost($table,$data) {
  715. $table_column = self::getFields($table);
  716. $newdata=array();
  717. foreach ($table_column as $key=>$val){
  718. if(array_key_exists($key,$data) && ($data[$key])!==''){
  719. $newdata[$key] = $data[$key];
  720. }
  721. }
  722. return $newdata;
  723. }
  724.  
  725. /**
  726. * 启动事务
  727. * @access function
  728. * @return void
  729. */
  730. static function startTrans() {
  731. //数据rollback 支持
  732. $link = self::$link;
  733. if ( !$link ) return false;
  734. if (self::$transTimes == 0) {
  735. $link->beginTransaction();
  736. }
  737. self::$transTimes++;
  738. return ;
  739. }
  740.  
  741. /**
  742. * 用于非自动提交状态下面的查询提交
  743. * @access function
  744. * @return boolen
  745. */
  746. static function commit() {
  747. $link = self::$link;
  748. if ( !$link ) return false;
  749. if (self::$transTimes > 0) {
  750. $result = $link->commit();
  751. self::$transTimes = 0;
  752. if(!$result){
  753. self::throw_exception(self::$error());
  754. return false;
  755. }
  756. }
  757. return true;
  758. }
  759.  
  760. /**
  761. * 事务回滚
  762. * @access function
  763. * @return boolen
  764. */
  765. public function rollback() {
  766. $link = self::$link;
  767. if ( !$link ) return false;
  768. if (self::$transTimes > 0) {
  769. $result = $link->rollback();
  770. self::$transTimes = 0;
  771. if(!$result){
  772. self::throw_exception(self::$error());
  773. return false;
  774. }
  775. }
  776. return true;
  777. }
  778.  
  779. /**
  780. * 错误处理
  781. * @access function
  782. * @return void
  783. */
  784. static function throw_exception($err){
  785. echo '<div style="width:500px;background-color:#CDCDCD; color:#A00;font-size:14px;border:1px #D40000 solid; margin:2px;padding:6px;">ERROR:'.$err.'</div>';
  786. }
  787. }

pdoModel封装的更多相关文章

  1. [C#] 简单的 Helper 封装 -- RegularExpressionHelper

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. iOS开发之App间账号共享与SDK封装

    上篇博客<iOS逆向工程之KeyChain与Snoop-it>中已经提到了,App间的数据共享可以使用KeyChian来实现.本篇博客就实战一下呢.开门见山,本篇博客会封装一个登录用的SD ...

  3. Ajax实现原理,代码封装

    都知道实现页面的异步操作需要使用Ajax,那么Ajax到是怎么实现异步操作的呢? 首先需要认识一个对象 --> XMLHttpRequest 对象 --> Ajax的核心.它有许多的属性和 ...

  4. 用C语言封装OC对象(耐心阅读,非常重要)

    用C语言封装OC对象(耐心阅读,非常重要) 本文的主要内容来自这里 前言 做iOS开发的朋友,对OC肯定非常了解,那么大家有没有想过OC中NSInteger,NSObject,NSString这些对象 ...

  5. 【知识必备】RxJava+Retrofit二次封装最佳结合体验,打造懒人封装框架~

    一.写在前面 相信各位看官对retrofit和rxjava已经耳熟能详了,最近一直在学习retrofit+rxjava的各种封装姿势,也结合自己的理解,一步一步的做起来. 骚年,如果你还没有掌握ret ...

  6. 对百度WebUploader开源上传控件的二次封装,精简前端代码(两句代码搞定上传)

    前言 首先声明一下,我这个是对WebUploader开源上传控件的二次封装,底层还是WebUploader实现的,只是为了更简洁的使用他而已. 下面先介绍一下WebUploader 简介: WebUp ...

  7. 封装集合(Encapsulate Collection)

    封装就是将相关的方法或者属性抽象成为一个对象. 封装的意义: 对外隐藏内部实现,接口不变,内部实现自由修改. 只返回需要的数据和方法. 提供一种方式防止数据被修改. 更好的代码复用. 当一个类的属性类 ...

  8. CSharpGL(29)初步封装Texture和Framebuffer

    +BIT祝威+悄悄在此留下版了个权的信息说: CSharpGL(29)初步封装Texture和Framebuffer +BIT祝威+悄悄在此留下版了个权的信息说: Texture和Framebuffe ...

  9. CSharpGL(7)对VAO和VBO的封装

    CSharpGL(7)对VAO和VBO的封装 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharpGL源码中包含10多个独立的Demo,更适合入门参考 ...

随机推荐

  1. 自动make工具--autotools

    自动生成Makefile GNU提供的autoconf和automake两套工具可自动完成符合自由软件惯例的makefile的编写.这样就可以像常见的GNU程序一样,只要使用“./configure” ...

  2. Help Tomisu UVA - 11440 难推导+欧拉函数,给定正整数N和M, 统计2和N!之间有多少个整数x满足,x的所有素因子都大于M (2<=N<=1e7, 1<=M<=N, N-M<=1E5) 输出答案除以1e8+7的余数。

    /** 题目:Help Tomisu UVA - 11440 链接:https://vjudge.net/problem/UVA-11440 题意:给定正整数N和M, 统计2和N!之间有多少个整数x满 ...

  3. AAC终结者Opus音频编码器的瑞士军刀,编译android ios

    AAC-LD/ELD it is either 480 or 512 PCM samples per frame and channel. http://opus-codec.org/download ...

  4. Java进阶03 IO基础(转载)

    IO示例 下面是演示的文件file.txt Hello World! Hello Nerd! 先来研究一个文件读取的例子: import java.io.*;public class Test{ pu ...

  5. 第一百七十六节,jQuery,插件

    jQuery,插件 学习要点: 1.插件概述 2.验证插件 3.自动完成插件 4.自定义插件 插件(Plugin)也成为 jQuery 扩展(Extension),是一种遵循一定规范的应用程序接口编 ...

  6. CentOS下使用MyTop实时监控MySQL

    CentOS下使用MyTop实时监控MySQL MyTop的项目页面为:http://jeremy.zawodny.com/mysql/mytop/ MyTop安装 $ yum -y install ...

  7. MFC多国语言——配置文件

    前段时间,因工作需要,本地化了一个英文版本的产品. 在网上查阅了若干资料,在此进行一个简单的整理. 在MFC程序中,实现多国语言的方式很多,我们选择的是使用配置文件的方法. 在通过配置文件方式实现多国 ...

  8. iOS - url中文和特殊字符转码###

    - (NSString *)generateUrl:(NSString *)url{ /** 第一个参数:NULL 第二个参数:C语言的字符串 第三个参数:NULL 第四个参数:要转义的字符串,不要乱 ...

  9. 使用神器MobaXterm连接远程mysql和redis

    https://mobaxterm.mobatek.net/download-home-edition.html mysql redis 连接测试 mysql 127.0.0.1 3307 密码使用线 ...

  10. python学习【第二篇】初识python

    python的安装 windows 1.下载安装包 https://www.python.org/downloads/ 2.安装 默认安装路径:C:\python27 3.配置环境变量 [右键计算机] ...