Phppdo encapsulation class to add, query, modify, and delete mysql data

Source: Internet
Author: User
Phppdo encapsulation class to add, query, modify, and delete mysql data

  1. Define ('dsn ', 'MySQL: host = 127.0.0.1; dbname = baozhong_tour'); // database address + constant of database name

  2. Define ('Db _ username', 'root'); // database USERNAME
  3. Define ('Db _ userpwd', ''); // database password
  4. ?>
  5. /**
  6. * @ Author shuimugan
  7. * @ Version 0.2
  8. * @ Return PDOStatement
  9. */
  10. Function getConn (){
  11. Try {
  12. $ Db = new PDO (DSN, DB_USERNAME, DB_USERPWD); // Create a pdo object and input database parameter information
  13. $ Db-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION); // you can specify a database error message.
  14. $ Db-> query ('set names utf8; '); // sets the encoding to utf8.
  15. Return $ db;
  16. }
  17. Catch (PDOException $ e ){
  18. Echo 'data processing error. please contact the website administrator! ';
  19. Print_r ($ e); // detailed error message. this sentence must be commented out after going online!
  20. Return false;
  21. }
  22. }
  23. /**
  24. * @ Author shuimugan
  25. * @ Version 0.2
  26. * @ Param String $ tablename table name, Type: String
  27. * @ Param Array $ column name of column_name, the format of the Array
  28. * @ Param String $ condition, for example, 'where name =? And pws =? 'Or 'name like? '
  29. * @ Param Array $ condition_value indicates the data corresponding to the condition. the type must be an Array, for example, 'Name =? And pws =? ,
  30. * @ Return Array returns an Array of result sets.
  31. * @ Example $ column_name = array ('*');
  32. $ Condition_value = array ('1 ');
  33. $ Limit = 'limit ';
  34. $ Res = easy_select ('user', $ column_name, 'where id =? ', $ Condition_value, $ limit );
  35. */
  36. Function easy_select ($ tablename, $ column_name, $ condition, $ condition_value, $ limit ){
  37. Try {
  38. $ Db = getConn ();
  39. $ SQL = 'select'; // Initialize an SQL statement
  40. Foreach ($ column_name as $ col_name ){
  41. // Dynamically append a column name to an SQL statement
  42. If ($ col_name = '*'){
  43. $ SQL. = '*';
  44. Break;
  45. }
  46. If ($ col_name = end ($ column_name ))
  47. {
  48. $ SQL. = $ col_name. ""; // if it belongs to the last one, the statement is not concatenated with a comma
  49. } Else {
  50. $ SQL. = $ col_name. ","; // splice column name + comma
  51. }
  52. }
  53. $ SQL. = "from". $ tablename. ""; // concatenate a table name
  54. $ SQL. = $ condition. ''; // concatenate a condition statement
  55. If (strlen ($ limit)> 0 ){
  56. $ SQL. = $ limit; // concatenate a limit statement
  57. }
  58. $ Db = new PDO (DSN, DB_USERNAME, DB_USERPWD); // Create a pdo object and input database parameter information
  59. $ Db-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION); // you can specify a database error message.
  60. $ Db-> query ('set names utf8; '); // sets the encoding to utf8.
  61. $ Stmt = $ db-> prepare ($ SQL );
  62. $ I = 1; // start counting, computing? Number
  63. $ J = count ($ condition_value );
  64. For (; $ I <= $ j ;){
  65. $ Stmt-> bindParam ($ I, $ condition_value [$ i-1]); // bind the parameter
  66. $ I ++;
  67. }
  68. // Query
  69. $ Stmt-> setFetchMode (PDO: FETCH_ASSOC); // you can specify a field to obtain data.
  70. $ Stmt-> execute ();
  71. // Obtain data
  72. Return $ stmt-> fetchAll ();
  73. // Return $ stmt-> rowCount ();
  74. } Catch (PDOException $ e ){
  75. Return false;
  76. Echo 'data processing error. please contact the website administrator! ';
  77. Print_r ($ e); // detailed error message. this sentence must be commented out after going online!
  78. }
  79. }
  80. /**
  81. * @ Author shuimugan
  82. * @ Version 0.2
  83. * @ Param String $ tablename table name, Type: String
  84. * @ Param Array $ column name of column_name, the format of the Array
  85. * @ Param String $ condition, for example, 'where name =? And pws =? 'Or 'name like? '
  86. * @ Param Array $ condition_value indicates the data corresponding to the condition. the type must be an Array, for example, 'Name =? And pws =? ,
  87. * @ Return int the number of returned Records
  88. * @ Example $ column_name = array ('*');
  89. $ Condition_value = array ('1 ');
  90. $ Limit = 'limit ';
  91. $ Res = easy_selectCount ('user', $ column_name, 'where id =? ', $ Condition_value, $ limit );
  92. */
  93. Function easy_selectCount ($ tablename, $ column_name, $ condition, $ condition_value, $ limit ){
  94. Try {
  95. $ Db = getConn ();
  96. $ SQL = 'select'; // Initialize an SQL statement
  97. Foreach ($ column_name as $ col_name ){
  98. // Dynamically append a column name to an SQL statement
  99. If ($ col_name = '*'){
  100. $ SQL. = '*';
  101. Break;
  102. }
  103. If ($ col_name = end ($ column_name ))
  104. {
  105. $ SQL. = $ col_name. ""; // if it belongs to the last one, the statement is not concatenated with a comma
  106. } Else {
  107. $ SQL. = $ col_name. ","; // splice column name + comma
  108. }
  109. }
  110. $ SQL. = "from". $ tablename. ""; // concatenate a table name
  111. $ SQL. = $ condition. ''; // concatenate a condition statement
  112. If (strlen ($ limit)> 0 ){
  113. $ SQL. = $ limit; // concatenate a limit statement
  114. }
  115. ;
  116. $ Db = new PDO (DSN, DB_USERNAME, DB_USERPWD); // Create a pdo object and input database parameter information
  117. $ Db-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION); // you can specify a database error message.
  118. $ Db-> query ('set names utf8; '); // sets the encoding to utf8.
  119. $ Stmt = $ db-> prepare ($ SQL );
  120. $ I = 1; // start counting, computing? Number
  121. $ J = count ($ condition_value );
  122. For (; $ I <= $ j ;){
  123. $ Stmt-> bindParam ($ I, $ condition_value [$ i-1]); // bind the parameter
  124. $ I ++;
  125. }
  126. // Query
  127. // $ Stmt-> setFetchMode (PDO: FETCH_ASSOC); // you can specify a field to obtain data.
  128. $ Stmt-> execute ();
  129. // Obtain data
  130. Return $ stmt-> rowCount ();
  131. } Catch (PDOException $ e ){
  132. Return false;
  133. Echo 'data processing error. please contact the website administrator! ';
  134. Print_r ($ e); // detailed error message. this sentence must be commented out after going online!
  135. }
  136. }
  137. /**
  138. * @ Author shuimugan
  139. * @ Version 0.2
  140. * @ Param String $ tablename table name, Type: String
  141. * @ Param Array $ column name of column_name, the format of the Array
  142. * @ Param Array $ column_value indicates the data corresponding to the Array format.
  143. * @ Return int returns the last auto-increment id.
  144. * @ Example $ column_name = array ('pwd', 'IP', 'name ');
  145. $ Column_value = array ('20140901', '11. 11.11.11 ', 'Zhang San ');
  146. Echo easy_insert ('user', $ column_name, $ column_value );
  147. */
  148. Function easy_insert ($ tablename, $ column_name, $ column_value ){
  149. Try {
  150. $ Db = getConn ();
  151. $ SQL = 'insert'; // Initialize an SQL statement
  152. $ SQL. = $ tablename. '('; // concatenate the table name
  153. Foreach ($ column_name as $ col_name ){
  154. // Dynamically append a column name to an SQL statement
  155. If ($ col_name = end ($ column_name ))
  156. {
  157. $ SQL. = $ col_name. ")"; // if it belongs to the last one, splice the right brackets
  158. } Else {
  159. $ SQL. = $ col_name. ","; // splice column name + comma
  160. }
  161. }
  162. $ SQL. = 'values ('; // concatenate $ condition_value statement
  163. For ($ I = 0; $ I <count ($ column_name); $ I ++) {// question mark?
  164. If ($ I = count ($ column_name)-1 ){
  165. $ SQL. = '?) ;';
  166. } Else {
  167. $ SQL. = '?, ';
  168. }
  169. }
  170. $ Db = new PDO (DSN, DB_USERNAME, DB_USERPWD); // Create a pdo object and input database parameter information
  171. $ Db-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION); // you can specify a database error message.
  172. $ Db-> query ('set names utf8; '); // sets the encoding to utf8.
  173. $ Stmt = $ db-> prepare ($ SQL );
  174. For ($ I = 1; $ I <= count ($ column_name); $ I ++) {// question mark?
  175. $ Stmt-> bindParam ($ I, $ column_value [$ i-1]);
  176. }
  177. // Query
  178. // Echo $ SQL;
  179. $ Stmt-> setFetchMode (PDO: FETCH_ASSOC); // you can specify a field to obtain data.
  180. $ Stmt-> execute ();
  181. // Obtain data
  182. Return $ db-> lastInsertId ();
  183. } Catch (PDOException $ e ){
  184. Return false;
  185. Echo 'data processing error. please contact the website administrator! ';
  186. Print_r ($ e); // detailed error message. this sentence must be commented out after going online!
  187. }
  188. }
  189. /**
  190. * @ Author shuimugan
  191. * @ Version 0.2
  192. * @ Param String $ tablename table name, Type: String
  193. * @ Param Array $ column name of column_name, the format of the Array
  194. * @ Param Array $ column_value indicates the data corresponding to the Array format.
  195. * @ Param String $ THE where statement String corresponding to condition is like 'Where id =? '
  196. * @ Param Array $ condition_value: the data type of the question mark after where must be an Array.
  197. * @ Return int returns the number of affected data rows.
  198. * @ Example $ column_name = array ('pwd', 'IP', 'name ');
  199. $ Column_value = array ('20140901', '192. 152.123.132 ', 'Zhang San ');
  200. $ Condition_value = array ('1 ');
  201. Easy_update ('user', $ column_name, $ column_value, 'where id =? ', $ Condition_value, null );
  202. */
  203. Function easy_update ($ tablename, $ column_name, $ column_value, $ condition, $ condition_value, $ limit ){
  204. Try {
  205. $ Db = getConn ();
  206. $ SQL = 'update'; // Initialize an SQL statement
  207. $ SQL. = $ tablename. 'set'; // list the table names.
  208. Foreach ($ column_name as $ col_name ){
  209. // Dynamically append a column name to an SQL statement
  210. If ($ col_name = end ($ column_name ))
  211. {
  212. $ SQL. = $ col_name. "=? "; // If it belongs to the last
  213. } Else {
  214. $ SQL. = $ col_name. "= ?, "; // Splice column names with commas (,)
  215. }
  216. }
  217. $ SQL. = $ condition; // concatenate a condition statement
  218. If (strlen ($ limit)> 0 ){
  219. $ SQL. = $ limit; // concatenate a limit statement
  220. }
  221. $ Db = new PDO (DSN, DB_USERNAME, DB_USERPWD); // Create a pdo object and input database parameter information
  222. $ Db-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION); // you can specify a database error message.
  223. $ Db-> query ('set names utf8; '); // sets the encoding to utf8.
  224. $ Stmt = $ db-> prepare ($ SQL); // prepare the statement
  225. $ I = 1;
  226. $ Total = count ($ column_name) + count ($ condition_value );
  227. For (; $ I <= count ($ column_name );){
  228. $ Stmt-> bindParam ($ I, $ column_value [$ i-1]); // bind the column name to the corresponding data parameter
  229. $ I ++;
  230. }
  231. $ J = 1;
  232. For (; $ I <= $ total ;){
  233. $ Stmt-> bindParam ($ I, $ condition_value [$ j-1]); // The Data parameter corresponding to the binding condition
  234. $ I ++;
  235. $ J ++;
  236. }
  237. // Query
  238. $ Stmt-> setFetchMode (PDO: FETCH_ASSOC); // you can specify a field to obtain data.
  239. $ Stmt-> execute ();
  240. // Obtain data
  241. Return $ stmt-> rowCount ();
  242. } Catch (PDOException $ e ){
  243. Return false;
  244. Echo 'data processing error. please contact the website administrator! ';
  245. Print_r ($ e); // detailed error message. this sentence must be commented out after going online!
  246. }
  247. }
  248. /**
  249. * @ Author shuimugan
  250. * @ Version 0.2
  251. * @ Param String $ tablename table name
  252. * @ Param String $ condition such as 'Where id =? '
  253. * @ Param Array $ the value corresponding to the condition_value condition is? Corresponding value
  254. * @ Param String $ limit statement, for example, 'Limit'
  255. * @ Return int returns the number of affected results
  256. * @ Example $ condition_value = array ('83 ');
  257. Echo easy_delete ('user', 'where id =? ', $ Condition_value, null );
  258. */
  259. Function easy_delete ($ tablename, $ condition, $ condition_value, $ limit ){
  260. Try {
  261. $ Db = getConn ();
  262. $ SQL = 'delete from'; // Initialize an SQL statement
  263. $ SQL. = $ tablename. ''; // list the table names.
  264. $ SQL. = $ condition; // concatenate a condition statement
  265. If (strlen ($ limit)> 0 ){
  266. $ SQL. = $ limit; // concatenate a limit statement
  267. }
  268. $ Db = new PDO (DSN, DB_USERNAME, DB_USERPWD); // Create a pdo object and input database parameter information
  269. $ Db-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION); // you can specify a database error message.
  270. $ Db-> query ('set names utf8; '); // sets the encoding to utf8.
  271. $ Stmt = $ db-> prepare ($ SQL); // prepare the statement
  272. For ($ I = 1; $ I <= count ($ condition_value );){
  273. $ Stmt-> bindParam ($ I, $ condition_value [$ i-1]); // The Data parameter corresponding to the binding condition
  274. $ I ++;
  275. }
  276. // Query
  277. $ Stmt-> setFetchMode (PDO: FETCH_ASSOC); // you can specify a field to obtain data.
  278. $ Stmt-> execute ();
  279. // Obtain data
  280. Return $ stmt-> rowCount ();
  281. } Catch (PDOException $ e ){
  282. Return false;
  283. Echo 'data processing error. please contact the website administrator! ';
  284. Print_r ($ e); // detailed error message. this sentence must be commented out after going online!

  285. }

  286. }
  287. ?>

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.