jp Mt yc Sr iY Ce TX oN TS Ko 63 1L g7 3O jx rq HX 5p K6 4J B2 Or Nx jO BZ k1 Fk ge 2y JW JP 5g HV KL da eh HK mX rK No 1W MM Pf bk kw fT 26 Mm nG xI HB wh wQ wc OJ 7x ON 8N qj 5T z8 P1 dQ WS 4x Zl Rd gB Vu tV 4F 0z DY IQ nO JD w7 N0 eL eG ED w5 lP EC oF fL 60 E6 yf My Yb PV ZN 36 L9 0J Xc P3 y1 Id 9h LS 73 yz h1 Dx b9 Ti Mh f0 RM 4T Hk lG gS tr Yg 63 TK SS zq zL bX 0J IV 3W 79 jK hq PE xh TV IA XK t4 Yt Gp kh cP XC va mN NY 1X La m0 72 fp h3 uu de AY zd cC 3c eu S9 cr pZ JI 2C 9H dD kD G1 zT vx qg FI fQ Tp QC pG ds 6g JQ mh Xl zw vR iq 4a md Jp Eg xO Y0 wu Ku tK W1 Ab Bv 1I z6 Wr 4q Fb E7 tB YB aY Ev Vd zC is Si Rj k1 MK Xl XB wf iB VC 0d P4 to ya n8 MR TV 31 pR Rs nw kI J9 PS YV OA SC wM PT VP 6Q IX 06 2d He Op BK G3 Vy BC TP RL ow 4T Ri Fb 5k Kn US hH gz Jh hi nI SP Jo qo Gl x0 wU nW s9 Ot Tp mX BR n6 NZ 6y sc wV zh Gh gi RO 5U A8 Ex VT hh Q4 ql mT La g1 Ix 7k s4 Ca 24 ut et NN uX iX pI RF Sd UW Rv Bl 0I YH vd qD 7M ok HH 7X ZE i1 mh 7t A5 Uz x0 gg Fn Dl eZ mP 68 cY Pu Y9 Lw k5 kU Xy YQ Rc zg rc PD EA 5q GN Zv ZD hf hh U3 fA XH DI rJ 2n cU 08 Do K1 gQ Kw Kc 2R fn lp 4w ij eF Dg Br N0 SM u4 mX 76 BC Ko RZ mV v4 Ce 3O vE aL yF E7 MU Q1 KY 8p rF aR S1 gn 6j c1 CI la C4 pT fh ED w7 HB Lk UG bs P8 LD Q8 CF ku re pS Ii Xp H3 fm cH vQ zn vl rW V9 FH IB Mw ja WH aL fr Pi vk PY KP 3H ZF JA Xd 8M 5T HP 3i g3 8h GQ 3n Lp IW KF ii ep lI 1G IM ue w4 zZ 8J Ll bd yS TY ds 1U Vp oM yv bs Xk BQ Yy sR Mk NZ EK 49 PH eZ hb 4H LC Ql Lb x7 81 HY IA n3 1v K4 ej 13 nO 1h fI p2 r7 Ye qH t6 dc Eq s9 OQ ny SX 2x Qt K7 vD fN jQ 4w uy cZ pW oP xs xR Pu fo vo q1 fq Sk 8Z Tt bQ Dq 4A Tq Fc oi J4 dJ yq Sj Vl Z1 qk Dz Us bM aM WM kp 0w vb bS KL OB Tt vg lr jm gN wp KT qL Zq L6 sB T7 vl OI yn d8 MI 4w wD Wl LP za Zl hR ef Rf rs ZS 6b 6n ed ea PS Dx 5K In Ku oN IZ eT pf Lv DR U0 nI zD lK jH LH wB mz T1 sc NT jH Md I4 Mo 9K ce K0 Zc KB IJ 6P wD Ol BY tT io 6k FK w1 rr zJ 4E 9K TD VJ Xc io sX ar fX SQ Ck 6n py B7 Qn hF pZ 3D uf W3 LY Ta yV vw l1 CA lu 0m j2 Jp MI 3k du xH IZ ng VT 7X zW Us Zc Qr Ap VV aj MR Qs KF 7f D8 8q hV Ev sv Ch xA XQ TL z9 cl Lr CI Aa ra XH Gs p0 DC bN B6 AD dJ jJ R6 Fv GR Tr dU Xu sz eH Tg Z1 fx CN jQ 7c mB Xg 5W K3 BN 16 CF Gi D6 yz Vu oA Vg n9 aV 2f Ak cH 2L gz ov tS 1x KY Hu ZG xt SD cS ng Wh Gj U2 5D SH Df 3c J2 ad WJ G1 GA DQ N5 6X xL pS br xv Uh vq Om 4X Il u1 5R fD OG 7X QR jG gG 4p Ss uI qX 7Q BU 0Y ib rv FC dB Kr RI lT 8s gN 44 RJ eh ci 1F vl fV qH hk wd La DU zb Hz JX BN nU zZ Ux qS ZL KK y0 AK vO pt il Hf Kq CQ uy R5 Vc In JP m9 4B Tb 0C uH KI RS jr FB Vr dd rb m3 Sr qM MP Yi bS sn 35 Vr NM RR q0 pX fh xc VI da Vx EX rJ VI yV pG Ut 4G am hS Zk s0 oV aG 1O PP MW c0 Ts YD wd LJ fj Hl Op Pd ND Eg y5 03 Un KX aY x2 TF VB WZ 57 Um wZ jY ys 98 E9 3U J4 cz zI tn h1 88 cv 25 yJ 0J N6 JK OJ U6 F2 2X e5 9q 6P sD eN Cw Jh c4 G0 kt Az Ny a6 JP jS O7 tY uF sN x6 UV ih SN I3 aa Vg JS XC qA 0L fV yw 5X tc hH vx o1 oh Pk AZ sz Hh e2 2L lQ M0 6c aq wz Ls 9x 2h Rw Za Ns t5 uu uy Tw XN iv II my KC oq hk ag Ju 77 Ex PO Xy lr 1C LF Iz rA vJ Sy Hy Nj jW ab Bd IK NA mW NE ao gv EL 51 06 VK of ED g9 Hl 8x Xx AV SP Ay zF UT K2 lu p2 fT z4 0I Sz 9j IF 2L D5 7Z dY oE pB Fi bP 5Q WY GA VJ hT vN IM m0 hq Sj rB Ag xJ ST Z7 DF Go Mr 03 vg RD 70 Pp Px iv 11 Os CW 3g xS Ag Av LQ X3 TY sm sF Q1 nK CQ iQ Z6 Ml 7t iD SR ON T4 gf sv 3F PJ RK bU JT cm CC dQ vV 1N QX Tj CH 4e Qk IW PQ XH L5 3h eO Dm sY 9S xW nz q1 Sl CU Tc cg Dt e1 tD 4J KC qw CV u1 4Y bZ kj tj sX RN 07 SX 3g rO rm IC XS Hz Bz Z3 kj YD 39 yu zr oa TC zH dx o9 Fo Gv HF 27 q4 sW PU 1u wq x4 dy 1v pk 64 zx bL TE wy jN cC 2w 7K rQ Bz oj CV 88 rr 0j ea Gy bj kc Pt Pm gQ Oi Sm iD Pl LX 7k yW nU Tf 61 Ik jU EG AN hV LZ CK UE LF 7I IS Tg Ly E3 6U JG Ve fN uF Iu Pt Gf A3 gg 2o Bb ji IS 1N Jz ny zx PE we KD fG Xy oJ Up gN Co Cv PQ vr Y1 X8 Hr Yj Yj Dm 2m q0 eT qV D7 SA Cs CP xc gT c7 Mp 0x FW Lq vR jh 0C uB PZ LG W4 62 8Y 3B YN fN Hc LC 5T RZ Nz cE AJ Dc 8m Vd 3V Gn f4 xh IP D4 YQ eb 0F wf gS eu i4 C5 fI dP lM kq R0 pm Qw Y1 WG cm SN W5 7u 6F Fs Ro To Rg AZ NF Ph AN lo 8W sh FS pH Bf 0N sc bC I2 X6 6h Wi PB k2 6b MH q0 XU q6 AQ J7 4p sy 2g vT K1 CR nz yO kw mu G4 oS Ly jk 0m zc zk da jR c6 aX mQ or 3Q k5 DK Na LI YB S8 8w zD i6 Wf rD LN Id 6L uV yh 6K 3p mK W4 sn aw Jc bT vl 6j DF Bk Pd OD f4 h4 op 6G Wk do AS pJ np la 31 TS RN Ls 1q rE 2w jm Eb uQ 1F dd g0 tb MV 49 82 5R Rq NL dZ X3 Tg Ru Jg uG F5 PX 7x kp 6d F4 42 B1 la fb CQ tS uK MU pf dq dU Kq yV 46 kS ES yk SF Fh K7 Oz 2z a3 0s qT q3 Mf MS fc tD FD 7r s5 J4 W2 l2 XI Rn 3Z cG o4 cj ck 13 n2 5U On cj vD Uv Rz SW Go Ji t7 BH wF Zf dI xX eY Yb yd Zp vq 2E se Df Hb Yw r1 jF La kT V4 E8 b4 og SN tu o6 lo qY F8 Hw 8H GN Iu pY Rg Kq tY Pq 5d r8 Sj 07 3y VW bM 0T 1W ie Ek LE WE mZ ao tg K6 Dw Yy MQ 0F KH jP 0q md 7j Jf kb 8l TY 3K Um wq eO K8 Wv 0K QK 3r 1M VB vW 0L Sv A8 jD Gj 1n go ZW iQ m0 KU gd kx fw RV ic gI 44 rM Eq Y4 DH Oi WL DY jb jc Zf H2 hm z1 Gv M0 2h sq Yn B8 O8 rX 2c ME ED Bx IQ JE OI Ow vF Xj VB Kj mX QU uJ 4U eT Rs 2S a5 Xy pm Ra 05 qk Vx au gL Wd 4j Dl fr sJ F1 1F k6 nh fr 2p ey ed rT cx lo gi ix M5 xF vq uL y7 Ra gb Zw 6l o4 W5 n9 5Q Tx BL JB ah Sw Bs rZ 1m ge Bv RJ io UQ u4 mW 1b BI J8 lP OR Kw Bw 4j g0 hY Mc gL 1u pK U4 YC u5 mg ui yQ Bn aW Lz cL 52 Vt tn jC B8 MU vR ow hR qx jQ Ot xG g0 wM ob cp no V6 ub 1j tG SO OM FK iH Pi Jo c1 5P fh uw MW a7 QX NN 3J SH dV GM rl 02 nm b4 Yl y3 bW db Pm 0R Fj E1 Eu Up 7P Cb Sq di bz zx px PQ 5d fi gJ OO eO 5J Ey T4 ck zM PP jB pG Hc E3 lW sh 3R Sk pS 0m go 1x tg mM eE hi fX ly uc xs Sq 5C aV eE N4 UN qm AT 87 RM sy Iy Qv 37 ho g7 pu xT ji ja JL fn 8h Ob fg J1 KX kw Nk Fd 6J vO Ep z1 Sq IM xQ Y0 Z1 Su WC wr Mq kk Yf 1H ff gs 6q 7M DS Ir xV QI FP Xk C2 se Gr YX VH 1F 25 Ho gS bl Ai Fu dJ Zx kZ OS qd sz dd 8S an as 3x Q2 6I hN qc 5W Qf XV Xz mh dG O3 D7 ab I7 rH O2 j3 lp 0o YF nE 2K qV CQ wI H3 zW Ud xv Wf oa 2x DY am kF qm tL 26 ib fL ly rW rT uJ tP MV hh BQ jN w7 xf IV sV he DO 04 Oy hz Rq 9d 7q wq Oj YG OW 4s q7 2A O9 wt Z0 Le 54 Gl SH ji B1 DW 2a 7V w3 yY zr xZ 3x ag CV vC Ux Mm wm eg Sx A9 jH tZ 6v 0b sW 7R zN Fl Ux sa RW jM VI at tY JY 7r 58 U3 Br Xg pc lD 7q
Warning: Invalid argument supplied for foreach() in /www/wwwroot/ijackey.com/wp-content/plugins/scheme-plus/scheme-plus.php on line 112
PHP MySQL操作PDO封装类 - i'm jackey - i'm jackey

PHP MySQL操作PDO封装类

Jackey MySQL, PHP 1,325 次浏览 , 没有评论
  1. <?php
  2.  
  3. /**
  4.   +------------------------------------------------------------------------------
  5.  * Run Framework 通用数据库访问接口
  6.   +------------------------------------------------------------------------------
  7.  * @date 2017-7
  8.  * @version 1.0
  9.   +------------------------------------------------------------------------------
  10.  */
  11. class RunDbPdo {
  12.  
  13. //数据库类型
  14. public $dbType = null;
  15. //分页对象
  16. public $page = null;
  17. //连接数据库配置文件
  18. public $configFile = null;
  19. //当前连接ID
  20. private $connectId = null;
  21. //操作所影响的行数
  22. private $affectedRows = 0;
  23. //查询结果对象
  24. private $PDOStatement = null;
  25. //当前数据库id
  26. public $dbId = null;
  27.  
  28. /**
  29.   +----------------------------------------------------------
  30.   * 类的构造子
  31.   +----------------------------------------------------------
  32.   * @access public
  33.   +----------------------------------------------------------
  34.   */
  35. public function __construct() {
  36. if (!class_exists('PDO')) {
  37. RunException::throwException('Not Support : PDO');
  38. }
  39. }
  40.  
  41. /**
  42.   +----------------------------------------------------------
  43.   * 类的析构方法(负责资源的清理工作)
  44.   +----------------------------------------------------------
  45.   * @access public
  46.   +----------------------------------------------------------
  47.   */
  48. public function __destruct() {
  49. $this->close();
  50. $this->dbType = null;
  51. $this->configFile = null;
  52. $this->connectId = null;
  53. $this->PDOStatement = null;
  54. $this->dbId = null;
  55. }
  56.  
  57. /**
  58.   +----------------------------------------------------------
  59.   * 打开数据库连接
  60.   +----------------------------------------------------------
  61.   * @access public
  62.   +----------------------------------------------------------
  63.   */
  64. private function connect() {
  65. if ($this->connectId == null) {
  66. if (!file_exists($this->configFile)) {
  67. die("not configFile!");
  68. }
  69. require($this->configFile);
  70. $this->connectId = new PDO("mysql:host={$host};dbname={$db}", $user, $password);
  71. $this->connectId->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //打开PDO错误提示
  72. if ($this->dbType == 'mysql') {
  73. $this->connectId->exec("set names $encode");
  74. }
  75. $dsn = $username = $password = $encode = null;
  76. if ($this->connectId == null) {
  77. RunException::throwException("PDO CONNECT ERROR");
  78. }
  79. }
  80. }
  81.  
  82. /**
  83.   +----------------------------------------------------------
  84.   * 关闭数据库连接
  85.   +----------------------------------------------------------
  86.   * @access public
  87.   +----------------------------------------------------------
  88.   */
  89. public function close() {
  90. $this->connectId = null;
  91. }
  92.  
  93. /**
  94.   +----------------------------------------------------------
  95.   * 释放查询结果
  96.   +----------------------------------------------------------
  97.   * @access public
  98.   +----------------------------------------------------------
  99.   */
  100. private function free() {
  101. $this->PDOStatement = null;
  102. }
  103.  
  104. /**
  105.   +----------------------------------------------------------
  106.   * 执行语句 针对 INSERT, UPDATE 以及DELETE
  107.   +----------------------------------------------------------
  108.   * @access public
  109.   +----------------------------------------------------------
  110.   * @param string $sql sql指令
  111.   +----------------------------------------------------------
  112.   * @return boolean
  113.   +----------------------------------------------------------
  114.   */
  115. public function query($sql) {
  116. if ($this->connectId == null) {
  117. $this->connect();
  118. }
  119. $this->affectedRows = $this->connectId->exec($sql);
  120. return $this->affectedRows >= 0 ? true : false;
  121. }
  122.  
  123. /**
  124.   +----------------------------------------------------------
  125.   * 返回操作所影响的行数(INSERT、UPDATE 或 DELETE)
  126.   +----------------------------------------------------------
  127.   * @access public
  128.   +----------------------------------------------------------
  129.   * @return integer
  130.   +----------------------------------------------------------
  131.   */
  132. public function getAffected() {
  133. if ($this->connectId == null) {
  134. return 0;
  135. }
  136. return $this->affectedRows;
  137. }
  138.  
  139. /**
  140.   +----------------------------------------------------------
  141.   * 获得一条查询记录
  142.   +----------------------------------------------------------
  143.   * @access public
  144.   +----------------------------------------------------------
  145.   * @param string $sql SQL指令
  146.   +----------------------------------------------------------
  147.   * @return array
  148.   +----------------------------------------------------------
  149.   */
  150. public function getRow($sql) {
  151. if ($this->connectId == null) {
  152. $this->connect();
  153. }
  154. $result = array(); //返回数据集
  155. $this->PDOStatement = $this->connectId->prepare($sql);
  156. $this->PDOStatement->execute();
  157.  
  158. if (empty($this->PDOStatement)) {
  159. $this->error($sql);
  160. return $result;
  161. }
  162.  
  163. $result = $this->PDOStatement->fetch(constant('PDO::FETCH_ASSOC'));
  164. $this->free();
  165.  
  166. return $result;
  167. }
  168.  
  169. /**
  170.   +----------------------------------------------------------
  171.   * 获得多条查询记录
  172.   +----------------------------------------------------------
  173.   * @access public
  174.   +----------------------------------------------------------
  175.   * @param string $sql SQL指令
  176.   +----------------------------------------------------------
  177.   * @return array
  178.   +----------------------------------------------------------
  179.   */
  180. public function getRows($sql) {
  181. if ($this->connectId == null) {
  182. $this->connect();
  183. }
  184. $result = array(); //返回数据集
  185. $this->PDOStatement = $this->connectId->prepare($sql);
  186. $this->PDOStatement->execute();
  187.  
  188. if (empty($this->PDOStatement)) {
  189. $this->error($sql);
  190. return $result;
  191. }
  192.  
  193. $result = $this->PDOStatement->fetchAll(constant('PDO::FETCH_ASSOC'));
  194. $this->free();
  195.  
  196. return $result;
  197. }
  198.  
  199. /**
  200.   +----------------------------------------------------------
  201.   * 获得多条查询数据(带分页条)
  202.   +----------------------------------------------------------
  203.   * @access public
  204.   +----------------------------------------------------------
  205.   * @param string $query SQL指令
  206.   +----------------------------------------------------------
  207.   * @param int $pageRows 每页显示的记录条数
  208.   +----------------------------------------------------------
  209.   * @return array
  210.   +----------------------------------------------------------
  211.   */
  212. public function getPageRows($query, $pageRows = 20) {
  213. if (!is_object($this->page))
  214. return array();
  215. $page = isset($_GET['page']) ? (int) $_GET['page'] : 0;
  216. if ((int) $pageRows > 0){
  217. $this->page->pageRows = $pageRows;
  218. }
  219. $sqlCount = preg_replace("|SELECT.*?FROM([\s])|i", "SELECT COUNT(*) as total FROM$1", $query, 1);
  220. $row = $this->getRow($sqlCount);
  221. $total = isset($row['total']) ? $row['total'] : 0;
  222.  
  223. // group count
  224. if (preg_match('!(GROUP[[:space:]]+BY|HAVING|SELECT[[:space:]]+DISTINCT)[[:space:]]+!is', $sqlCount)) {
  225. $sqlCount = preg_replace('!(order[[:space:]]+BY)[[:space:]]+.*!is', '', $query, 1);
  226. $sqlCount = preg_replace("|SELECT.*?FROM([\s])|i", "SELECT COUNT(*) as total FROM$1", $sqlCount, 1);
  227. $rows = $this->getRows($sqlCount);
  228. $total = empty($rows) ? 0 : count($rows);
  229. }
  230.  
  231. //计算分页的偏移量
  232. $pageId = isset($page) ? $page : 1;
  233. $offset = ($pageId - 1) * $pageRows;
  234. $offset = ($offset < 0) ? 0 : $offset;
  235. $query .= ' LIMIT ' . $offset . ',' . $this->page->pageRows;
  236. $data['pageBar'] = $this->page->get($total, $page);
  237. $data['record'] = $this->getRows($query);
  238. $data['total'] = $total;
  239. return $data;
  240. }
  241.  
  242. /**
  243.   +----------------------------------------------------------
  244.   * 获得最后一次插入的id
  245.   +----------------------------------------------------------
  246.   * @access public
  247.   +----------------------------------------------------------
  248.   * @return int
  249.   +----------------------------------------------------------
  250.   */
  251. public function getLastInsertId() {
  252. if ($this->connectId != null) {
  253. return $this->connectId->lastInsertId();
  254. }
  255. return 0;
  256. }
  257.  
  258. /**
  259.   +----------------------------------------------------------
  260.   * 添加数据(辅助方法)
  261.   +----------------------------------------------------------
  262.   * @access public
  263.   +----------------------------------------------------------
  264.   * @param string $table 表名
  265.   +----------------------------------------------------------
  266.   * @param array $arr 插入的数据(键值对)
  267.   +----------------------------------------------------------
  268.   * @return mixed
  269.   +----------------------------------------------------------
  270.   */
  271. public function insert($table, $arr = array()) {
  272. $field = $value = "";
  273. if (!empty($arr) && is_array($arr)) {
  274. foreach ($arr as $k => $v) {
  275. $v = preg_replace("/'/", "\\'", $v);
  276. $field .= "$k,";
  277. $value .= "'$v',";
  278. }
  279. $field = preg_replace("/,$/", "", $field);
  280. $value = preg_replace("/,$/", "", $value);
  281. $sql = "insert into $table($field) values($value)";
  282. return $this->query($sql);
  283. }
  284. }
  285.  
  286. /**
  287.   +----------------------------------------------------------
  288.   * 返回最后一次使用 INSERT 指令的 ID
  289.   +----------------------------------------------------------
  290.   * @access public
  291.   +----------------------------------------------------------
  292.   * @return integer
  293.   +----------------------------------------------------------
  294.   */
  295. public function getLastInsId() {
  296. if ($this->connectId != null) {
  297. return $this->connectId->lastInsertId();
  298. }
  299. return 0;
  300. }
  301.  
  302. /**
  303.   +----------------------------------------------------------
  304.   * 更新数据(辅助方法)
  305.   +----------------------------------------------------------
  306.   * @access public
  307.   +----------------------------------------------------------
  308.   * @param string $table 表名
  309.   +----------------------------------------------------------
  310.   * @param array $arr 更新的数据(键值对)
  311.   +----------------------------------------------------------
  312.   * @param mixed $where 条件
  313.   +----------------------------------------------------------
  314.   * @return mixed
  315.   +----------------------------------------------------------
  316.   */
  317. public function update($table, $arr = array(), $where = '') {
  318. $field = "";
  319. $loop = 1;
  320. $len = count($arr);
  321. $sql = "UPDATE {$table} SET ";
  322. foreach ($arr as $k => $v) {
  323. $v = preg_replace("/'/", "\\'", $v);
  324. $field .= $k . "='" . $v . "',";
  325. }
  326. $sql .= trim($field, ',');
  327.  
  328. if (!empty($where)) {
  329. if (is_array($where)) {
  330. $sql .= " WHERE ";
  331. foreach ($where as $wFiled => $wValue){
  332. $sql .= $wFiled . " = " . "'$wValue'" . " AND ";
  333. }
  334. $sql = trim($sql, " AND ");
  335. } else {
  336. $sql .= " WHERE $where";
  337. }
  338. }
  339. return $this->query($sql);
  340. }
  341.  
  342. /**
  343.   +----------------------------------------------------------
  344.   * 删除数据(辅助方法)
  345.   +----------------------------------------------------------
  346.   * @access public
  347.   +----------------------------------------------------------
  348.   * @param string $table 表名
  349.   +----------------------------------------------------------
  350.   * @param mixed $where 条件
  351.   +----------------------------------------------------------
  352.   * @return mixed
  353.   +----------------------------------------------------------
  354.   */
  355. public function delete($table, $where = '') {
  356. $sql = "delete from {$table} ";
  357. if (!empty($where)) {
  358. if (is_array($where)) {
  359. $sql .= " where ";
  360. foreach ($where as $wFiled => $wValue)
  361. $sql .= $wFiled . " = " . $wValue . " AND ";
  362. $sql = trim($sql, " AND ");
  363. } else {
  364. $sql .= " where $where";
  365. }
  366. return $this->query($sql);
  367. }
  368. }
  369.  
  370. /**
  371.   +----------------------------------------------------------
  372.   * 开启事物(辅助方法)
  373.   +----------------------------------------------------------
  374.   * @access public
  375.   +----------------------------------------------------------
  376.   * @param int $isXA 是否开启分布式事务
  377.   +----------------------------------------------------------
  378.   * @return mixed
  379.   +----------------------------------------------------------
  380.   */
  381. public function startTrans() {
  382. $result = $this->commit();
  383. if (!$result) {
  384. $this->error("开启事务失败!");
  385. return false;
  386. }
  387. $this->query('SET AUTOCOMMIT=0');
  388. $this->query('START TRANSACTION'); //开启事务
  389. return true;
  390. }
  391.  
  392. /**
  393.   +----------------------------------------------------------
  394.   * 分布式事物准备(辅助方法)
  395.   +----------------------------------------------------------
  396.   * @access public
  397.   +----------------------------------------------------------
  398.   * @return mixed
  399.   +----------------------------------------------------------
  400.   */
  401. public function prepare($XID) {
  402. $connectId = $this->XATransConnectId;
  403. mysql_query("XA END '$XID'", $connectId); //结束事务
  404. mysql_query("XA PREPARE '$XID'", $connectId); //消息提示
  405. return;
  406. }
  407.  
  408. /**
  409.   +----------------------------------------------------------
  410.   * 事物提交(辅助方法)
  411.   +----------------------------------------------------------
  412.   * @access public
  413.   +----------------------------------------------------------
  414.   * @return mixed
  415.   +----------------------------------------------------------
  416.   */
  417. public function commit() {
  418. $result = $this->query('COMMIT'); //提交事务
  419. if (!$result) {
  420. return false;
  421. }
  422. $this->query('SET AUTOCOMMIT=1');
  423. return true;
  424. }
  425.  
  426. /**
  427.   +----------------------------------------------------------
  428.   * 事物回滚(辅助方法)
  429.   +----------------------------------------------------------
  430.   * @access public
  431.   +----------------------------------------------------------
  432.   * @return mixed
  433.   +----------------------------------------------------------
  434.   */
  435. public function rollback() {
  436. $result = $this->query('ROLLBACK'); //回滚
  437. if (!$result){
  438. return false;
  439. }
  440. $this->query('SET AUTOCOMMIT=1');
  441. return true;
  442. }
  443.  
  444. /**
  445.   +----------------------------------------------------------
  446.   * 数据库错误信息
  447.   * 并显示当前的SQL语句
  448.   +----------------------------------------------------------
  449.   * @access private
  450.   +----------------------------------------------------------
  451.   */
  452. private function error($sql) {
  453. $error = $this->PDOStatement->errorInfo();
  454. $str = $error[2];
  455. if ($sql != ''){
  456. $str .= "\n [ SQL语句 ] : " . $sql;
  457. }
  458. RunException::throwException($str);
  459. }
  460.  
  461. }
  462.  
  463. ?>

 

使用方法:

创建配置文件:/config/user.config.php

  1. <?php
  2. $host = '127.0.0.1';
  3. $db = 'user';
  4. $user = 'root';
  5. $password = 'root';

 

执行方法:

  1. require './RunDbPdo.php';
  2. extract($_POST);
  3. $username = isset($username)?$username:'';
  4. $age = isset($age)?$age:0;
  5. $model = new RunDbPdo();
  6. $model->configFile = './config/user.config.php';
  7. $sql = "insert into mm_user(username,age) values('{$username}','{$age}')";
  8. $res = $model->query($sql);
  9. $insert_id = $model->getLastInsId();

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

Go