PHP PDO database Operation encapsulation Class code

Source: Internet
Author: User
  1. /**

  2. * Database PDO operations
  3. */
  4. Class Mysqlpdo {
  5. public static $PDOStatement = null;
  6. /**
  7. * Configuration of database connection parameters
  8. * @var Array
  9. * @access Public
  10. */
  11. public static $config = Array ();
  12. /**
  13. * Whether to use permanent connection
  14. * @var BOOL
  15. * @access Public
  16. */
  17. public static $pconnect = false;
  18. /**
  19. * Error message
  20. * @var String
  21. * @access Public
  22. */
  23. public static $error = ';
  24. /**
  25. * Single-piece mode, saving the unique instance of PDO class, database connection resources
  26. * @var Object
  27. * @access Public
  28. */
  29. protected static $link;
  30. /**
  31. * Whether the database is already connected
  32. * @var BOOL
  33. * @access Public
  34. */
  35. public static $connected = false;
  36. /**
  37. * Database version
  38. * @var String
  39. * @access Public
  40. */
  41. public static $dbVersion = null;
  42. /**
  43. * Current SQL statement
  44. * @var String
  45. * @access Public
  46. */
  47. public static $queryStr = ';
  48. /**
  49. * ID of the last inserted record
  50. * @var Integer
  51. * @access Public
  52. */
  53. public static $lastInsertId = null;
  54. /**
  55. * Returns the number of affected records
  56. * @var Integer
  57. * @access Public
  58. */
  59. public static $numRows = 0;
  60. Number of transaction instructions
  61. public static $transTimes = 0;
  62. /**
  63. * Constructor function,
  64. * @param $dbconfig database connection related information, array (' ServerName ', ' UserName ', ' Password ', ' defaultdb ', ' db_port ', ' Db_type ')
  65. */
  66. Public function __construct ($dbConfig = ") {
  67. if (!class_exists (' PDO ')) self::throw_exception ("Not supported: PDO");
  68. If no parameters are transferred, the default data definition is used
  69. if (!is_array ($dbConfig)) {
  70. $dbConfig = Array (
  71. ' Hostname ' = db_host,
  72. ' Username ' = Db_user,
  73. ' Password ' = db_pwd,
  74. ' Database ' = db_name,
  75. ' Hostport ' = Db_port,
  76. ' DBMS ' = Db_type,
  77. ' DSN ' = = Db_type. ': host= '. Db_host. "; Dbname= ". Db_name
  78. );
  79. }
  80. if (Empty ($dbConfig [' hostname '])) self::throw_exception ("No database configuration defined");
  81. Self:: $config = $dbConfig;
  82. if (Empty (self:: $config [' params ']) "Self:: $config [' params '] = array ();
  83. /************************************* Gorgeous divider Line *******************************************/
  84. if (!isset (self:: $link)) {
  85. $configs = self:: $config;
  86. if (self:: $pconnect) {
  87. $configs [' params '][constant (' pdo::attr_persistent ')] = true;
  88. }
  89. try {
  90. Self:: $link = new PDO ($configs [' DSN '], $configs [' username '], $configs [' Password '], $configs [' params ']);
  91. } catch (Pdoexception $e) {
  92. Self::throw_exception ($e->getmessage ());
  93. }
  94. if (!self:: $link) {
  95. Self::throw_exception (' PDO CONNECT ERROR ');
  96. return false;
  97. }
  98. Self:: $link->exec (' SET NAMES '). Db_charset);
  99. Self:: $dbVersion = self:: $link->getattribute (Constant ("pdo::attr_server_info"));
  100. Token connection Succeeded
  101. Self:: $connected = true;
  102. Unregister Database Connection configuration information
  103. Unset ($configs);
  104. }
  105. Return self:: $link;
  106. }
  107. /**
  108. * Release Query Results
  109. * @access function
  110. */
  111. static function free () {
  112. Self:: $PDOStatement = null;
  113. }
  114. /************************/
  115. /* Database operation */
  116. /************************/
  117. /**
  118. * Get all the query data
  119. * @access function
  120. * @return Array
  121. */
  122. static function GetAll ($sql =null) {
  123. if ($sql! = null)
  124. {
  125. Self::query ($sql);
  126. }
  127. Return Data set
  128. $result = self:: $PDOStatement->fetchall (Constant (' pdo::fetch_assoc '));
  129. return $result;
  130. }
  131. /**
  132. * Get a query result
  133. * @access function
  134. * @param string $sql SQL instructions
  135. * @param integer $seek pointer position
  136. * @return Array
  137. */
  138. static function GetRow ($sql =null) {
  139. if ($sql! = null)
  140. {
  141. Self::query ($sql);
  142. }
  143. Return array Set
  144. $result = self:: $PDOStatement->fetch (Constant (' pdo::fetch_assoc '), constant (' pdo::fetch_ori_next '));
  145. return $result;
  146. }
  147. /**
  148. * Execute SQL statements, automatically determine queries or perform operations
  149. * @access function
  150. * @param string $sql SQL instructions
  151. * @return Mixed
  152. */
  153. static function Dosql ($sql = ") {
  154. if (Self::ismainips ($sql)) {
  155. Return Self::execute ($sql);
  156. }else {
  157. Return Self::getall ($sql);
  158. }
  159. }
  160. /**
  161. * Find records in table based on specified ID (only for single table operation)
  162. * @access function
  163. * @param integer $PRIID primary Key ID
  164. * @param string $tables data table name
  165. * @param string $fields field name
  166. * @return Arrayobject table record
  167. */
  168. static function FindByID ($tabName, $priId, $fields = ' * ') {
  169. $sql = ' SELECT%s from%s WHERE id=%d ';
  170. Return Self::getrow (sprintf ($sql, Self::p arsefields ($fields), $tabName, $priId));
  171. }
  172. /**
  173. * Find Records
  174. * @access function
  175. * @param string $tables data table name
  176. * @param mixed $where query conditions
  177. * @param string $fields field name
  178. * @param string $order sort
  179. * @param string $limit How many data to fetch
  180. * @param string $group grouping
  181. * @param string $having
  182. * @param boolean $lock is locked
  183. * @return Arrayobject
  184. */
  185. static function Find ($tables, $where = "", $fields = ' * ', $order =null, $limit =null, $group =null, $having =null) {
  186. $sql = ' SELECT '. Self::p arsefields ($fields)
  187. .' From '. $tables
  188. . Self::p arsewhere ($where)
  189. . Self::p arsegroup ($group)
  190. . Self::p arsehaving ($having)
  191. . Self::p arseorder ($order)
  192. . Self::p arselimit ($limit);
  193. $dataAll = Self::getall ($sql);
  194. if (count ($dataAll) ==1) {$rlt = $dataAll [0];} else{$rlt = $dataAll;}
  195. return $RLT;
  196. }
  197. /**
  198. * Insert (Single) record
  199. * @access function
  200. * @param mixed $data data
  201. * @param string $table data table name
  202. * @return False | Integer
  203. */
  204. function Add ($data, $table) {
  205. Filter submission Data
  206. $data =self::filterpost ($table, $data);
  207. foreach ($data as $key = = $val) {
  208. if (Is_array ($val) && strtolower ($val [0]) = = = ' Exp ') {
  209. $val = $val [1]; Use an expression???
  210. }elseif (Is_scalar ($val)) {
  211. $val = Self::fieldformat ($val);
  212. }else{
  213. Remove a Compound object
  214. Continue
  215. }
  216. $data [$key] = $val;
  217. }
  218. $fields = Array_keys ($data);
  219. Array_walk ($fields, Array ($this, ' Addspecialchar '));
  220. $FIELDSSTR = Implode (', ', $fields);
  221. $values = Array_values ($data);
  222. $VALUESSTR = Implode (', ', $values);
  223. $sql = ' INSERT into ' $table. ' ('. $fieldsStr. ') VALUES ('. $valuesStr. ') ';
  224. Return Self::execute ($sql);
  225. }
  226. /**
  227. * Update record
  228. * @access function
  229. * @param mixed $sets data
  230. * @param string $table data table name
  231. * @param string $where update condition
  232. * @param string $limit
  233. * @param string $order
  234. * @return False | Integer
  235. */
  236. static function Update ($sets, $table, $where, $limit =0, $order = ') {
  237. $sets = Self::filterpost ($table, $sets);
  238. $sql = ' UPDATE '. $table. ' SET '. Self::p arsesets ($sets). Self::p arsewhere ($where). Self::p arseorder ($order). Self::p arselimit ($limit);
  239. Return Self::execute ($sql);
  240. }
  241. /**
  242. * Save the value of a field
  243. * @access function
  244. * @param string $field The name of the field to be saved
  245. * @param string $value field value
  246. * @param string $table Data Sheet
  247. * @param string $where Save condition
  248. * @param boolean $asString whether the field value is a string
  249. * @return void
  250. */
  251. static function SetField ($field, $value, $table, $condition = "", $asString =false) {
  252. Update field contents as plain string if there is ' (' as a SQL instruction update)
  253. if (false = = = Strpos ($value, ' (') | | $asString) $value = ' "'. $value. '";
  254. $sql = ' UPDATE '. $table. ' SET '. $field. ' = '. $value. Self::p arsewhere ($condition);
  255. Return Self::execute ($sql);
  256. }
  257. /**
  258. * Delete Records
  259. * @access function
  260. * @param mixed $where as conditional map, array, or string
  261. * @param string $table data table name
  262. * @param string $limit
  263. * @param string $order
  264. * @return False | Integer
  265. */
  266. static function Remove ($where, $table, $limit = ", $order =") {
  267. $sql = "DELETE from". $table. Self::p arsewhere ($where). Self::p arseorder ($order). Self::p arselimit ($limit);
  268. Return Self::execute ($sql);
  269. }
  270. /**
  271. +----------------------------------------------------------
  272. * Modify or save data (only for single-table operation)
  273. * There is a primary key ID is modified, no primary key ID is added
  274. * Change record:
  275. +----------------------------------------------------------
  276. * @access function
  277. +----------------------------------------------------------
  278. * @param $tabName Table name
  279. * @param $aPost submit a form $_post
  280. * @param $priId primary Key ID
  281. * @param $aNot a field or array to exclude
  282. * @param $aCustom A custom array that is attached to the database to save
  283. * @param whether $isExits already exists: True, does not exist: false
  284. +----------------------------------------------------------
  285. * @return Boolean modified or saved successfully
  286. +----------------------------------------------------------
  287. */
  288. function Saveorupdate ($tabName, $aPost, $priId = "", $aNot = "", $aCustom = "", $isExits =false) {
  289. if (Empty ($tabName) | |!is_array ($aPost) | | is_int ($ANOT)) return false;
  290. if (is_string ($aNot) &&!empty ($aNot)) $aNot = Array ($aNot);
  291. if (Is_array ($aNot) && is_int (Key ($aNot))) $aPost = Array_diff_key ($aPost, Array_flip ($aNot));
  292. if (Is_array ($aCustom) && is_string (Key ($aCustom))) $aPost = Array_merge ($aPost, $aCustom);
  293. if (Empty ($priId) &&! $isExits) {//New
  294. $aPost = Array_filter ($aPost, Array ($this, ' removeempty '));
  295. Return Self::add ($aPost, $tabName);
  296. } else {//Modify
  297. Return Self::update ($aPost, $tabName, "id=". $priId);
  298. }
  299. }
  300. /**
  301. * Get the SQL statement for the most recent query
  302. * @access function
  303. * @param
  304. * SQL executed @return String
  305. */
  306. static function Getlastsql () {
  307. $link = self:: $link;
  308. if (! $link) return false;
  309. Return self:: $queryStr;
  310. }
  311. /**
  312. * Get the last inserted ID
  313. * @access function
  314. * @param
  315. * @return The Data ID when the last integer is inserted
  316. */
  317. static function Getlastinsid () {
  318. $link = self:: $link;
  319. if (! $link) return false;
  320. Return self:: $lastInsertId;
  321. }
  322. /**
  323. * Get DB version
  324. * @access function
  325. * @param
  326. * @return String
  327. */
  328. static function Getdbversion () {
  329. $link = self:: $link;
  330. if (! $link) return false;
  331. Return self:: $dbVersion;
  332. }
  333. /**
  334. * Get table information from the database
  335. * @access function
  336. * @return Array
  337. */
  338. static function Gettables () {
  339. $info = Array ();
  340. if (Self::query ("SHOW TABLES")) {
  341. $result = Self::getall ();
  342. foreach ($result as $key = = $val) {
  343. $info [$key] = current ($val);
  344. }
  345. }
  346. return $info;
  347. }
  348. /**
  349. * Get field information from the data table
  350. * @access function
  351. * @return Array
  352. */
  353. static function GetFields ($tableName) {
  354. Get database Join
  355. $link = self:: $link;
  356. $sql = "Select
  357. Ordinal_position, COLUMN_NAME, Column_type, Data_type,
  358. IF (ISNULL (Character_maximum_length), (Numeric_precision + Numeric_scale), character_maximum_length) as MAXCHAR,
  359. Is_nullable, Column_default, Column_key, EXTRA, column_comment
  360. From
  361. Information_schema. COLUMNS
  362. WHERE
  363. table_name =: TabName and Table_schema= ' ". Db_name. "'";
  364. Self:: $queryStr = sprintf ($sql, $tableName);
  365. $sth = $link->prepare ($sql);
  366. $sth->bindparam (': TabName ', $tableName);
  367. $sth->execute ();
  368. $result = $sth->fetchall (constant (' pdo::fetch_assoc '));
  369. $info = Array ();
  370. foreach ($result as $key = = $val) {
  371. $info [$val [' column_name ']] = Array (
  372. ' Postion ' = $val [' ordinal_position '],
  373. ' Name ' = $val [' column_name '],
  374. ' Type ' = $val [' Column_type '],
  375. ' D_type ' = $val [' Data_type '],
  376. ' Length ' = $val [' Maxchar '],
  377. ' Notnull ' = (strtolower ($val [' is_nullable ')] = = "No"),
  378. ' Default ' = $val [' Column_default '],
  379. ' Primary ' = (Strtolower ($val [' column_key ')] = = ' pri '),
  380. ' AutoInc ' = (strtolower ($val [' EXTRA ']) = = ' Auto_increment '),
  381. ' Comment ' = $val [' column_comment ']
  382. );
  383. }
  384. Throws an exception if there is an error
  385. Self::haveerrorthrowexception ();
  386. return $info;
  387. }
  388. /**
  389. * Close Database
  390. * @access function
  391. */
  392. static function Close () {
  393. Self:: $link = null;
  394. }
  395. /**
  396. * SQL command Security filtering
  397. * @access function
  398. * @param string $STR SQL instructions
  399. * @return String
  400. */
  401. static function escape_string ($STR) {
  402. Return addslashes ($STR);
  403. }
  404. /************************/
  405. /* Internal Operation method */
  406. /************************/
  407. /**
  408. * There is an error throwing exception
  409. * @access function
  410. * @return
  411. */
  412. static function Haveerrorthrowexception () {
  413. $obj = Empty (self:: $PDOStatement)? Self:: $link: Self:: $PDOStatement;
  414. $arrError = $obj->errorinfo ();
  415. if ($arrError [0]!== ' 00000 ') {//error message
  416. Self:: $error = $arrError [0]. "|". $arrError [2]. "
    [SQL]: ". Self:: $queryStr."
    ";
  417. Self::throw_exception (self:: $error);
  418. return false;
  419. }
  420. Throwing exceptions primarily for the Execute () method
  421. if (self:: $queryStr = = ") self::throw_exception (' Query was empty

    [SQL statement]: ');
  422. }
  423. /**
  424. * Where analysis
  425. * @access function
  426. * @param mixed $where query conditions
  427. * @return String
  428. */
  429. static function Parsewhere ($where) {
  430. $WHERESTR = ";
  431. if (is_string ($where) | | is_null ($where)) {
  432. $WHERESTR = $where;
  433. }
  434. return empty ($WHERESTR)? ': ' WHERE '. $whereStr;
  435. }
  436. /**
  437. * Order Analysis
  438. * @access function
  439. * Sort @param mixed $order
  440. * @return String
  441. */
  442. static function Parseorder ($order) {
  443. $ORDERSTR = ";
  444. if (Is_array ($order))
  445. $orderStr. = ' ORDER by '. Implode (', ', $order);
  446. else if (is_string ($order) &&!empty ($order))
  447. $orderStr. = ' ORDER by '. $order;
  448. return $orderStr;
  449. }
  450. /**
  451. * Limit Analysis
  452. * @access function
  453. * @param string $limit
  454. * @return String
  455. */
  456. static function Parselimit ($limit) {
  457. $LIMITSTR = ";
  458. if (Is_array ($limit)) {
  459. if (count ($limit) >1)
  460. $limitStr. = ' LIMIT '. $limit [0]. ', '. $limit [1]. ' ';
  461. Else
  462. $limitStr. = ' LIMIT '. $limit [0]. ' ';
  463. } else if (is_string ($limit) &&!empty ($limit)) {
  464. $limitStr. = ' LIMIT '. $limit. ' ';
  465. }
  466. return $limitStr;
  467. }
  468. /**
  469. * Group Analysis
  470. * @access function
  471. * @param mixed $group
  472. * @return String
  473. */
  474. static function Parsegroup ($group) {
  475. $GROUPSTR = ";
  476. if (Is_array ($group))
  477. $groupStr. = ' GROUP by '. Implode (', ', $group);
  478. else if (is_string ($group) &&!empty ($group))
  479. $groupStr. = ' GROUP by '. $group;
  480. return empty ($GROUPSTR)? ': $groupStr;
  481. }
  482. /**
  483. * Having analysis
  484. * @access function
  485. * @param string $having
  486. * @return String
  487. */
  488. static function parsehaving ($having) {
  489. $HAVINGSTR = ";
  490. if (is_string ($having) &&!empty ($having))
  491. $havingStr. = ' having '. $having;
  492. return $havingStr;
  493. }
  494. /**
  495. * Fields Analysis
  496. * @access function
  497. * @param mixed $fields
  498. * @return String
  499. */
  500. function Parsefields ($fields) {
  501. if (Is_array ($fields)) {
  502. Array_walk ($fields, Array ($this, ' Addspecialchar '));
  503. $FIELDSSTR = Implode (', ', $fields);
  504. }else if (is_string ($fields) &&!empty ($fields)) {
  505. if (false = = = Strpos ($fields, ' ")) {
  506. $fields = Explode (', ', $fields);
  507. Array_walk ($fields, Array ($this, ' Addspecialchar '));
  508. $FIELDSSTR = Implode (', ', $fields);
  509. }else {
  510. $FIELDSSTR = $fields;
  511. }
  512. }else $fieldsStr = ' * ';
  513. return $fieldsStr;
  514. }
  515. /**
  516. * Sets analysis, called when updating data
  517. * @access function
  518. * @param mixed $values
  519. * @return String
  520. */
  521. Private Function Parsesets ($sets) {
  522. $SETSSTR = ";
  523. if (Is_array ($sets)) {
  524. foreach ($sets as $key = = $val) {
  525. $key = Self::addspecialchar ($key);
  526. $val = Self::fieldformat ($val);
  527. $setsStr. = "$key =". $val. ",";
  528. }
  529. $SETSSTR = substr ($setsStr, 0,-1);
  530. }else if (is_string ($sets)) {
  531. $SETSSTR = $sets;
  532. }
  533. return $setsStr;
  534. }
  535. /**
  536. * Field formatting
  537. * @access function
  538. * @param mixed $value
  539. * @return Mixed
  540. */
  541. static function FieldFormat (& $value) {
  542. if (Is_int ($value)) {
  543. $value = Intval ($value);
  544. } else if (Is_float ($value)) {
  545. $value = Floatval ($value);
  546. } elseif (Preg_match ('/^\ (\w* (\+|\-|\*|\/) \w*\) $/i ', $value)) {
  547. Supports direct use of other fields in field values
  548. For example (score+1) (name) must contain parentheses
  549. $value = $value;
  550. }else if (is_string ($value)) {
  551. $value = ' \ '. self::escape_string ($value). ' \'';
  552. }
  553. return $value;
  554. }
  555. /**
  556. * Fields and table names are added ' compliant
  557. * Guarantee the use of keywords in the instruction is not wrong for MySQL
  558. * @access function
  559. * @param mixed $value
  560. * @return Mixed
  561. */
  562. static function Addspecialchar (& $value) {
  563. if (' * ' = = $value | | False!== strpos ($value, ' (') | | false!== strpos ($value, '. ') | | false!== strpos ($value, ")) {
  564. Do not process if include * or use SQL method
  565. } elseif (false = = = Strpos ($value, ")) {
  566. $value = ". Trim ($value). ' `';
  567. }
  568. return $value;
  569. }
  570. /**
  571. +----------------------------------------------------------
  572. * Remove empty elements
  573. +----------------------------------------------------------
  574. * @access function
  575. +----------------------------------------------------------
  576. * @param mixed $value
  577. +----------------------------------------------------------
  578. * @return Mixed
  579. +----------------------------------------------------------
  580. */
  581. static function Removeempty ($value) {
  582. Return!empty ($value);
  583. }
  584. /**
  585. * Execute the query mainly for SELECT, SHOW and other instructions
  586. * @access function
  587. * @param string $sql SQL instructions
  588. * @return Mixed
  589. */
  590. static function query ($sql = ") {
  591. Get database Join
  592. $link = self:: $link;
  593. if (! $link) return false;
  594. Self:: $queryStr = $sql;
  595. Release the previous query results
  596. if (!empty (self:: $PDOStatement)) Self::free ();
  597. Self:: $PDOStatement = $link->prepare (self:: $QUERYSTR);
  598. $bol = self:: $PDOStatement->execute ();
  599. Throws an exception if there is an error
  600. Self::haveerrorthrowexception ();
  601. return $bol;
  602. }
  603. /**
  604. * Database Operation method
  605. * @access function
  606. * @param string $sql Execute the statement
  607. * @param boolean $lock is locked (not locked by default)
  608. * @return void
  609. Public Function Execute ($sql = ', $lock =false) {
  610. if (empty ($sql)) $sql = $this->querystr;
  611. return $this->_execute ($sql);
  612. }*/
  613. /**
  614. * Execute statements for INSERT, UPDATE, and delete
  615. * @access function
  616. * @param string $sql SQL instructions
  617. * @return Integer
  618. */
  619. static function Execute ($sql = ") {
  620. Get database Join
  621. $link = self:: $link;
  622. if (! $link) return false;
  623. Self:: $queryStr = $sql;
  624. Release the previous query results
  625. if (!empty (self:: $PDOStatement)) Self::free ();
  626. $result = $link->exec (self:: $QUERYSTR);
  627. Throws an exception if there is an error
  628. Self::haveerrorthrowexception ();
  629. if (false = = = $result) {
  630. return false;
  631. } else {
  632. Self:: $numRows = $result;
  633. Self:: $lastInsertId = $link->lastinsertid ();
  634. Return self:: $numRows;
  635. }
  636. }
  637. /**
  638. * Whether to change operation for database
  639. * @access Private
  640. * @param string $query SQL instructions
  641. * @return Boolen If the query operation returns false
  642. */
  643. static function Ismainips ($query) {
  644. $queryIps = ' insert| update| delete| replace| create| drop| LOAD data| SELECT. * into| copy| alter| Grant| revoke| lock| UNLOCK ';
  645. if (Preg_match ('/^\s* ')? ('. $queryIps. ') \s+/i ', $query)) {
  646. return true;
  647. }
  648. return false;
  649. }
  650. /**
  651. * Filter Post submission data
  652. * @access Private
  653. * Submit data @param mixed $data post
  654. * @param string $table data table name
  655. * @return Mixed $newdata
  656. */
  657. static function Filterpost ($table, $data) {
  658. $table _column = Self::getfields ($table);
  659. $newdata =array ();
  660. foreach ($table _column as $key = + $val) {
  661. if (Array_key_exists ($key, $data) && ($data [$key])!== ') {
  662. $newdata [$key] = $data [$key];
  663. }
  664. }
  665. return $newdata;
  666. }
  667. /**
  668. * Start Transaction
  669. * @access function
  670. * @return void
  671. */
  672. static function Starttrans () {
  673. Data rollback support
  674. $link = self:: $link;
  675. if (! $link) return false;
  676. if (self:: $transTimes = = 0) {
  677. $link->begintransaction ();
  678. }
  679. Self:: $transTimes + +;
  680. return;
  681. }
  682. /**
  683. * For non-autocommit status, the following query is submitted
  684. * @access function
  685. * @return Boolen
  686. */
  687. static function commit () {
  688. $link = self:: $link;
  689. if (! $link) return false;
  690. if (self:: $transTimes > 0) {
  691. $result = $link->commit ();
  692. Self:: $transTimes = 0;
  693. if (! $result) {
  694. Self::throw_exception (self:: $error ());
  695. return false;
  696. }
  697. }
  698. return true;
  699. }
  700. /**
  701. * Transaction rollback
  702. * @access function
  703. * @return Boolen
  704. */
  705. Public Function rollback () {
  706. $link = self:: $link;
  707. if (! $link) return false;
  708. if (self:: $transTimes > 0) {
  709. $result = $link->rollback ();
  710. Self:: $transTimes = 0;
  711. if (! $result) {
  712. Self::throw_exception (self:: $error ());
  713. return false;
  714. }
  715. }
  716. return true;
  717. }

  718. /**

  719. * Error Handling
  720. * @access function
  721. * @return void
  722. */
  723. static function Throw_exception ($err) {
  724. Echo ' ERROR: '. $err. ';
  725. }
  726. }

Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.