国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Backend Development PHP Tutorial PDO database operation class

PDO database operation class

Jul 25, 2016 am 08:44 AM

  1. class HRDB{
  2. protected $pdo;
  3. protected $res;
  4. protected $config;
  5. /*constructor*/
  6. function __construct($config){
  7. $this->Config = $config;
  8. $this->connect();
  9. }
  10. /*Database connection*/
  11. public function connect(){
  12. $this->pdo = new PDO($this->Config['dsn'], $this->Config['name'], $this->Config['password']);
  13. $this->pdo->query('set names utf8;');
  14. //Put the result Serialized into stdClass
  15. //$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
  16. //Write your own code to catch Exception
  17. $this->pdo->setAttribute(PDO: :ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  18. }
  19. /*Database close*/
  20. public function close(){
  21. $this->pdo = null;
  22. }
  23. public function query($sql){
  24. $ res = $this->pdo->query($sql);
  25. if($res){
  26. $this->res = $res;
  27. }
  28. }
  29. public function exec($sql){
  30. $ res = $this->pdo->exec($sql);
  31. if($res){
  32. $this->res = $res;
  33. }
  34. }
  35. public function fetchAll(){
  36. return $this ->res->fetchAll();
  37. }
  38. public function fetch(){
  39. return $this->res->fetch();
  40. }
  41. public function fetchColumn(){
  42. return $this-> ;res->fetchColumn();
  43. }
  44. public function lastInsertId(){
  45. return $this->res->lastInsertId();
  46. }
  47. /**
  48. * Parameter Description
  49. * int $debug Whether to enable debugging, if enabled, the sql statement will be output
  50. * 0 Do not enable
  51. * 1 Enable
  52. * 2 Enable and terminate the program
  53. * int $mode Return type
  54. * 0 Return multiple records
  55. * 1 Returns a single record
  56. * 2 Returns the number of rows
  57. * string/array $table database table, two value-passing modes
  58. * Normal mode:
  59. * 'tb_member, tb_money'
  60. * Array mode:
  61. * array('tb_member', 'tb_money')
  62. * string/array $fields Database fields to be queried, allowed to be empty, default is to find all, two value-passing modes
  63. * Normal mode:
  64. * 'username, password'
  65. * Array mode:
  66. * array('username', 'password')
  67. * string/array $sqlwhere query conditions, empty allowed, two value-passing modes
  68. * Normal mode:
  69. * 'and type = 1 and username like "%os%"'
  70. * Array mode:
  71. * array('type = 1', 'username like "%os%"')
  72. * string $orderby sorting, the default is id reverse order
  73. */
  74. public function select( $debug, $mode, $table, $fields="*", $sqlwhere="", $orderby="tbid desc"){
  75. //Parameter processing
  76. if(is_array($table)){
  77. $table = implode(', ', $table);
  78. }
  79. if(is_array($fields)){
  80. $fields = implode(', ', $fields);
  81. }
  82. if(is_array($sqlwhere)){
  83. $ sqlwhere = ' and '.implode(' and ', $sqlwhere);
  84. }
  85. //Database operation
  86. if($debug === 0){
  87. if($mode === 2){
  88. $this-> ;query("select count(tbid) from $table where 1=1 $sqlwhere");
  89. $return = $this->fetchColumn();
  90. }else if($mode === 1){
  91. $this ->query("select $fields from $table where 1=1 $sqlwhere order by $orderby");
  92. $return = $this->fetch();
  93. }else{
  94. $this->query( "select $fields from $table where 1=1 $sqlwhere order by $orderby");
  95. $return = $this->fetchAll();
  96. }
  97. return $return;
  98. }else{
  99. if($mode = == 2){
  100. echo "select count(tbid) from $table where 1=1 $sqlwhere";
  101. }else if($mode === 1){
  102. echo "select $fields from $table where 1=1 $sqlwhere order by $orderby";
  103. }
  104. else{
  105. echo "select $fields from $table where 1=1 $sqlwhere order by $orderby";
  106. }
  107. if($debug === 2){
  108. exit;
  109. }
  110. }
  111. }
  112. /**
  113. * Parameter Description
  114. * int $debug Whether to enable debugging, if enabled, the sql statement will be output
  115. * 0 Do not enable
  116. * 1 Enable
  117. * 2 Enable and terminate the program
  118. * int $mode Return type
  119. * 0 No return information
  120. * 1 Returns the number of execution entries
  121. * 2 Returns the id of the last inserted record
  122. * string/array $table database table, two value-passing modes
  123. * Normal mode:
  124. * 'tb_member, tb_money'
  125. * Array mode:
  126. * array( 'tb_member', 'tb_money')
  127. * string/array $set Fields and contents to be inserted, two value-passing modes
  128. * Normal mode:
  129. * 'username = "test", type = 1, dt = now() '
  130. * Array mode:
  131. * array('username = "test"', 'type = 1', 'dt = now()')
  132. */
  133. public function insert($debug, $mode, $table, $set){
  134. //參數(shù)處理
  135. if(is_array($table)){
  136. $table = implode(', ', $table);
  137. }
  138. if(is_array($set)){
  139. $set = implode(', ', $set);
  140. }
  141. //數(shù)據(jù)庫(kù)操作
  142. if($debug === 0){
  143. if($mode === 2){
  144. $this->query("insert into $table set $set");
  145. $return = $this->lastInsertId();
  146. }else if($mode === 1){
  147. $this->exec("insert into $table set $set");
  148. $return = $this->res;
  149. }else{
  150. $this->query("insert into $table set $set");
  151. $return = NULL;
  152. }
  153. return $return;
  154. }else{
  155. echo "insert into $table set $set";
  156. if($debug === 2){
  157. exit;
  158. }
  159. }
  160. }
  161. /**
  162. * Parameter Description
  163. * int $debug Whether to enable debugging, if enabled, the sql statement will be output
  164. * 0 Do not enable
  165. * 1 Enable
  166. * 2 Enable and terminate the program
  167. * int $mode Return type
  168. * 0 No return information
  169. * 1 Returns the number of execution entries
  170. * string $table database table, two value-passing modes
  171. * Normal mode:
  172. * 'tb_member, tb_money'
  173. * Array mode:
  174. * array('tb_member', 'tb_money')
  175. * string/ array $set Fields and contents that need to be updated, two value-passing modes
  176. * Normal mode:
  177. * 'username = "test", type = 1, dt = now()'
  178. * Array mode:
  179. * array('username = "test"', 'type = 1', 'dt = now()')
  180. * string/array $sqlwhere Modify the conditions, allow empty, two value-passing modes
  181. * Normal mode:
  182. * 'and type = 1 and username like "%os%"'
  183. * Array mode:
  184. * array('type = 1', 'username like "%os%"')
  185. */
  186. public function update($debug, $mode, $table, $set, $sqlwhere=""){
  187. //參數(shù)處理
  188. if(is_array($table)){
  189. $table = implode(', ', $table);
  190. }
  191. if(is_array($set)){
  192. $set = implode(', ', $set);
  193. }
  194. if(is_array($sqlwhere)){
  195. $sqlwhere = ' and '.implode(' and ', $sqlwhere);
  196. }
  197. //數(shù)據(jù)庫(kù)操作
  198. if($debug === 0){
  199. if($mode === 1){
  200. $this->exec("update $table set $set where 1=1 $sqlwhere");
  201. $return = $this->res;
  202. }else{
  203. $this->query("update $table set $set where 1=1 $sqlwhere");
  204. $return = NULL;
  205. }
  206. return $return;
  207. }else{
  208. echo "update $table set $set where 1=1 $sqlwhere";
  209. if($debug === 2){
  210. exit;
  211. }
  212. }
  213. }
  214. /**
  215. * Parameter Description
  216. * int $debug Whether to enable debugging, if enabled, the sql statement will be output
  217. * 0 Do not enable
  218. * 1 Enable
  219. * 2 Enable and terminate the program
  220. * int $mode Return type
  221. * 0 No return information
  222. * 1 Returns the number of execution entries
  223. * string $table database table
  224. * string/array $sqlwhere deletion condition, allowed to be empty, two value-passing modes
  225. * Normal mode:
  226. * 'and type = 1 and username like "%os%" '
  227. * Array mode:
  228. * array('type = 1', 'username like "%os%"')
  229. */
  230. public function delete($debug, $mode, $table, $sqlwhere=""){
  231. //參數(shù)處理
  232. if(is_array($sqlwhere)){
  233. $sqlwhere = ' and '.implode(' and ', $sqlwhere);
  234. }
  235. //數(shù)據(jù)庫(kù)操作
  236. if($debug === 0){
  237. if($mode === 1){
  238. $this->exec("delete from $table where 1=1 $sqlwhere");
  239. $return = $this->res;
  240. }else{
  241. $this->query("delete from $table where 1=1 $sqlwhere");
  242. $return = NULL;
  243. }
  244. return $return;
  245. }else{
  246. echo "delete from $table where 1=1 $sqlwhere";
  247. if($debug === 2){
  248. exit;
  249. }
  250. }
  251. }
  252. }
復(fù)制代碼

PDO


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are some best practices for versioning a PHP-based API? What are some best practices for versioning a PHP-based API? Jun 14, 2025 am 12:27 AM

ToversionaPHP-basedAPIeffectively,useURL-basedversioningforclarityandeaseofrouting,separateversionedcodetoavoidconflicts,deprecateoldversionswithclearcommunication,andconsidercustomheadersonlywhennecessary.StartbyplacingtheversionintheURL(e.g.,/api/v

How do I implement authentication and authorization in PHP? How do I implement authentication and authorization in PHP? Jun 20, 2025 am 01:03 AM

TosecurelyhandleauthenticationandauthorizationinPHP,followthesesteps:1.Alwayshashpasswordswithpassword_hash()andverifyusingpassword_verify(),usepreparedstatementstopreventSQLinjection,andstoreuserdatain$_SESSIONafterlogin.2.Implementrole-basedaccessc

What are the differences between procedural and object-oriented programming paradigms in PHP? What are the differences between procedural and object-oriented programming paradigms in PHP? Jun 14, 2025 am 12:25 AM

Proceduralandobject-orientedprogramming(OOP)inPHPdiffersignificantlyinstructure,reusability,anddatahandling.1.Proceduralprogrammingusesfunctionsorganizedsequentially,suitableforsmallscripts.2.OOPorganizescodeintoclassesandobjects,modelingreal-worlden

What are weak references (WeakMap) in PHP, and when might they be useful? What are weak references (WeakMap) in PHP, and when might they be useful? Jun 14, 2025 am 12:25 AM

PHPdoesnothaveabuilt-inWeakMapbutoffersWeakReferenceforsimilarfunctionality.1.WeakReferenceallowsholdingreferenceswithoutpreventinggarbagecollection.2.Itisusefulforcaching,eventlisteners,andmetadatawithoutaffectingobjectlifecycles.3.YoucansimulateaWe

How can you handle file uploads securely in PHP? How can you handle file uploads securely in PHP? Jun 19, 2025 am 01:05 AM

To safely handle file uploads in PHP, the core is to verify file types, rename files, and restrict permissions. 1. Use finfo_file() to check the real MIME type, and only specific types such as image/jpeg are allowed; 2. Use uniqid() to generate random file names and store them in non-Web root directory; 3. Limit file size through php.ini and HTML forms, and set directory permissions to 0755; 4. Use ClamAV to scan malware to enhance security. These steps effectively prevent security vulnerabilities and ensure that the file upload process is safe and reliable.

How can you interact with NoSQL databases (e.g., MongoDB, Redis) from PHP? How can you interact with NoSQL databases (e.g., MongoDB, Redis) from PHP? Jun 19, 2025 am 01:07 AM

Yes, PHP can interact with NoSQL databases like MongoDB and Redis through specific extensions or libraries. First, use the MongoDBPHP driver (installed through PECL or Composer) to create client instances and operate databases and collections, supporting insertion, query, aggregation and other operations; second, use the Predis library or phpredis extension to connect to Redis, perform key-value settings and acquisitions, and recommend phpredis for high-performance scenarios, while Predis is convenient for rapid deployment; both are suitable for production environments and are well-documented.

What are the differences between == (loose comparison) and === (strict comparison) in PHP? What are the differences between == (loose comparison) and === (strict comparison) in PHP? Jun 19, 2025 am 01:07 AM

In PHP, the main difference between == and == is the strictness of type checking. ==Type conversion will be performed before comparison, for example, 5=="5" returns true, and ===Request that the value and type are the same before true will be returned, for example, 5==="5" returns false. In usage scenarios, === is more secure and should be used first, and == is only used when type conversion is required.

How do I perform arithmetic operations in PHP ( , -, *, /, %)? How do I perform arithmetic operations in PHP ( , -, *, /, %)? Jun 19, 2025 pm 05:13 PM

The methods of using basic mathematical operations in PHP are as follows: 1. Addition signs support integers and floating-point numbers, and can also be used for variables. String numbers will be automatically converted but not recommended to dependencies; 2. Subtraction signs use - signs, variables are the same, and type conversion is also applicable; 3. Multiplication signs use * signs, which are suitable for numbers and similar strings; 4. Division uses / signs, which need to avoid dividing by zero, and note that the result may be floating-point numbers; 5. Taking the modulus signs can be used to judge odd and even numbers, and when processing negative numbers, the remainder signs are consistent with the dividend. The key to using these operators correctly is to ensure that the data types are clear and the boundary situation is handled well.

See all articles