Php pdo database operations

Source: Internet
Author: User
Php pdo database operations

A simple PDO class encapsulation .. Learning and communication only

PdoDb database class

  1. /**
  2. * @ Throws Error
  3. * PDO database
  4. */
  5. Class PdoDb extends DatabaseAbstract
  6. {
  7. /**
  8. * PDO instance
  9. * @ Var PDO
  10. */
  11. Protected $ DB;
  12. /**
  13. * PDO preparation statement
  14. * @ Var PDOStatement
  15. */
  16. Protected $ Stmt;
  17. /**
  18. * The final SQL statement
  19. * @ Var string
  20. */
  21. Protected $ SQL;
  22. /**
  23. * Configuration information $ config = array ('dsn '=> xxx, 'name' => xxx, 'password' => xxx, 'option' => xxx)
  24. * @ Var array
  25. */
  26. Protected $ Config;
  27. /**
  28. * Constructor
  29. * @ Param array $ config
  30. */
  31. Public function _ construct ($ config)
  32. {
  33. $ This-> Config = $ config;
  34. }
  35. /**
  36. * Connect to the database
  37. * @ Return void
  38. */
  39. Public function connect ()
  40. {
  41. $ This-> DB = new PDO ($ this-> Config ['dsn '], $ this-> Config ['name'], $ this-> Config ['password'], $ this-> Config ['option']);
  42. // Serialize the result to stdClass by default
  43. $ This-> DB-> setAttribute (PDO: ATTR_DEFAULT_FETCH_MODE, PDO: FETCH_OBJ );
  44. // Write your own code to catch Exception
  45. $ This-> DB-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_SILENT );
  46. }
  47. /**
  48. * Disconnect
  49. * @ Return void
  50. */
  51. Public function disConnect ()
  52. {
  53. $ This-> DB = null;
  54. $ This-> Stmt = null;
  55. }
  56. /**
  57. * Execute the SQL statement and return the newly added id.
  58. * @ Param string $ statement
  59. * @ Return string
  60. */
  61. Public function exec ($ statement)
  62. {
  63. If ($ this-> DB-> exec ($ statement )){
  64. $ This-> SQL = $ statement;
  65. Return $ this-> lastId ();
  66. }
  67. $ This-> errorMessage ();
  68. }
  69. /**
  70. * Querying SQL
  71. * @ Param string $ statement
  72. * @ Return PdoDb
  73. */
  74. Public function query ($ statement)
  75. {
  76. $ Res = $ this-> DB-> query ($ statement );
  77. If ($ res ){
  78. $ This-> Stmt = $ res;
  79. $ This-> SQL = $ statement;
  80. Return $ this;
  81. }
  82. $ This-> errorMessage ();
  83. }
  84. /**
  85. * Serialize data once
  86. * @ Return mixed
  87. */
  88. Public function fetchOne ()
  89. {
  90. Return $ this-> Stmt-> fetch ();
  91. }
  92. /**
  93. * Serialize all data
  94. * @ Return array
  95. */
  96. Public function fetchAll ()
  97. {
  98. Return $ this-> Stmt-> fetchAll ();
  99. }
  100. /**
  101. * Last added id
  102. * @ Return string
  103. */
  104. Public function lastId ()
  105. {
  106. Return $ this-> DB-> lastInsertId ();
  107. }
  108. /**
  109. * Number of affected rows
  110. * @ Return int
  111. */
  112. Public function affectRows ()
  113. {
  114. Return $ this-> Stmt-> rowCount ();
  115. }
  116. /**
  117. * Prepared statement
  118. * @ Param string $ statement
  119. * @ Return PdoDb
  120. */
  121. Public function prepare ($ statement)
  122. {
  123. $ Res = $ this-> DB-> prepare ($ statement );
  124. If ($ res ){
  125. $ This-> Stmt = $ res;
  126. $ This-> SQL = $ statement;
  127. Return $ this;
  128. }
  129. $ This-> errorMessage ();
  130. }
  131. /**
  132. * Bind data
  133. * @ Param array $ array
  134. * @ Return PdoDb
  135. */
  136. Public function bindArray ($ array)
  137. {
  138. Foreach ($ array as $ k => $ v ){
  139. If (is_array ($ v )){
  140. // Effective structure of array ('value' => xxx, 'type' => PDO: PARAM_XXX)
  141. $ This-> Stmt-> bindValue ($ k + 1, $ v ['value'], $ v ['type']);
  142. } Else {
  143. $ This-> Stmt-> bindValue ($ k + 1, $ v, PDO: PARAM_STR );
  144. }
  145. }
  146. Return $ this;
  147. }
  148. /**
  149. * Execute the prepared statement
  150. * @ Return bool
  151. */
  152. Public function execute ()
  153. {
  154. If ($ this-> Stmt-> execute ()){
  155. Return true;
  156. }
  157. $ This-> errorMessage ();
  158. }
  159. /**
  160. * Start a transaction
  161. * @ Return bool
  162. */
  163. Public function beginTransaction ()
  164. {
  165. Return $ this-> DB-> beginTransaction ();
  166. }
  167. /**
  168. * Execute transactions
  169. * @ Return bool
  170. */
  171. Public function commitTransaction ()
  172. {
  173. Return $ this-> DB-> commit ();
  174. }
  175. /**
  176. * Roll back a transaction
  177. * @ Return bool
  178. */
  179. Public function rollbackTransaction ()
  180. {
  181. Return $ this-> DB-> rollBack ();
  182. }
  183. /**
  184. * Throw an error.
  185. * @ Throws Error
  186. * @ Return void
  187. */
  188. Public function errorMessage ()
  189. {
  190. $ Msg = $ this-> DB-> errorInfo ();
  191. Throw new Error ('database Error: '. $ msg [2]);
  192. }
  193. //---------------------
  194. /**
  195. * Singleton instance
  196. * @ Var PdoDb
  197. */
  198. Protected static $ _ instance;
  199. /**
  200. * Default database
  201. * @ Static
  202. * @ Param array $ config
  203. * @ Return PdoDb
  204. */
  205. Public static function instance ($ config)
  206. {
  207. If (! Self: $ _ instance instanceof PdoDb ){
  208. Self: $ _ instance = new PdoDb ($ config );
  209. Self: $ _ instance-> connect ();
  210. }
  211. Return self: $ _ instance;
  212. }
  213. //----------------------
  214. /**
  215. * Obtain databases supported by PDO
  216. * @ Static
  217. * @ Return array
  218. */
  219. Public static function getSupportDriver (){
  220. Return PDO: getAvailableDrivers ();
  221. }
  222. /**
  223. * Obtain the database version information.
  224. * @ Return array
  225. */
  226. Public function getDriverVersion (){
  227. $ Name = $ this-> DB-> getAttribute (PDO: ATTR_DRIVER_NAME );
  228. Return array ($ name => $ this-> DB-> getAttribute (PDO: ATTR_CLIENT_VERSION ));
  229. }
  230. }

When using

  1. PdoDb: instance ($ config );

PHP, PDO

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.