搜尋
首頁後端開發php教程PHP PDO封装静态类代码分享

  1. /**
  2. * Class DB
  3. * 数据库操作类
  4. */
  5. class DB {
  6. /**
  7. * @var
  8. * @return CDB
  9. */
  10. private static $db;
  11. /**获取CDb类
  12. * @param $table_name 表名称
  13. * @param string $db_setting 调用数据库配置项
  14. * @param array $db_config 数据库配置
  15. * @return CDb
  16. */
  17. public static function cdb($table_name='',$db_setting='default',$db_config=array()){
  18. if(!isset(self::$db)){
  19. $db = new CDb($table_name,$db_setting,$db_config);
  20. self::$db=$db;
  21. }else{
  22. $db=self::$db;
  23. }
  24. return $db;
  25. }
  26. /** 配置
  27. * @param $table_name 表名称
  28. * @param string $db_setting 调用数据库配置项
  29. * @param array $db_config 数据库配置
  30. * @return CDb
  31. */
  32. public static function init($table_name='',$db_setting='default',$db_config=array()) {
  33. return self::cdb($table_name,$db_setting,$db_config);
  34. }
  35. /**
  36. * 执行删除记录操作
  37. * @param $table 表名称
  38. * @param $condition 删除数据条件,不充许为空。可以为数组
  39. * @return boolean
  40. */
  41. public static function delete($table, $condition) {
  42. $db=self::cdb();
  43. $db->setTableName($table);
  44. return $db->delete($condition);
  45. }
  46. /**
  47. * 执行添加记录操作
  48. * @param $table 表名称
  49. * @param array $data要增加的数据,参数为数组。数组key为字段值,数组值为数据取值
  50. * @param bool $return_insert_id 是否返回新建ID号
  51. * @param bool $replace 是否采用 replace into的方式添加数据
  52. * @return boolean
  53. */
  54. public static function insert($table, $data, $return_insert_id = false, $replace = false) {
  55. $db=self::cdb();
  56. $db->setTableName($table);
  57. return $db->insert($data, $return_insert_id, $replace);
  58. }
  59. /**
  60. * 获取最后一次添加记录的主键号
  61. * @return int
  62. */
  63. public static function insertID() {
  64. $db=self::cdb();
  65. return $db->insert_id();
  66. }
  67. /**
  68. * 执行更新记录操作
  69. * @param $table 表名称
  70. * @param $data 要更新的数据内容,参数为数组
  71. * 为数组时数组key为字段值,数组值为数据取值
  72. * 为数组时[例: array('name'=>'lanmps','password'=>'123456')]
  73. * 数组的另一种使用array('name'=>'+=1', 'base'=>'-=1');程序会自动解析为`name` = `name` + 1, `base` = `base` - 1
  74. * 字符串,请按照格式 :
  75. * 字符串 [例2:array('catid=:catid AND time>=:time ',array(':catid'=>10,':time'=>'2012-02-10')) ]
  76. * @param $where 更新数据时的条件,
  77. * 字符串,请按照格式 :
  78. * 字符串 [例1:" id=1 and time>$time " ]
  79. * 字符串 [例2:array('catid=:catid AND time>=:time ',array(':catid'=>10,':time'=>'2012-02-10')) ]
  80. * 数组时 [例: array('name'=>'lanmps','password'=>'123456')]
  81. * @return boolean
  82. */ bbs.it-home.org
  83. public static function update($table, $data, $where) {
  84. $db=self::cdb();
  85. $db->setTableName($table);
  86. return $db->update($data,$where);
  87. }
  88. /**
  89. * 获取单条记录查询
  90. * @param array $sql 查询条件语句
  91. * @return array/null数据查询结果集,如果不存在,则返回空
  92. */
  93. public static function fetchFirst($sql) {
  94. $db=self::cdb();
  95. return $db->fetch($sql);
  96. }
  97. /**
  98. * 执行sql查询
  99. * @param $sql查询条件
  100. * @return array 查询结果集数组
  101. */
  102. public static function fetchAll($sql) {
  103. $db=self::cdb();
  104. return $db->fetchAll($sql);
  105. }
  106. /**
  107. * 直接执行sql查询
  108. * @param $sql 查询sql语句
  109. * @return
  110. */
  111. public static function query($sql) {
  112. $db=self::cdb();
  113. return $db->exec($sql);
  114. }
  115. /**
  116. * 执行sql查询
  117. * @param $table 表名称
  118. * @param $where 查询条件
  119. * 字符串,请按照格式 :
  120. * 字符串 [例1:" id=1 and time>$time " ]
  121. * 字符串 [例2:array('catid=:catid AND time>=:time ',array(':catid'=>10,':time'=>'2012-02-10')) ]
  122. * 数组时 [例: array('name'=>'lanmps','password'=>'123456')]
  123. * @param $fields需要查询的字段值[例`name`,`gender`,`birthday`]
  124. * @param $limit 返回结果范围[例:10或10,10 默认为空]
  125. * @param $order 排序方式 [默认按数据库默认方式排序]
  126. * @param $group 分组方式 [默认为空]
  127. * @return array 查询结果集数组
  128. */
  129. public static function select($table,$where = '', $fields = '*', $limit = '', $order = '', $group = '') {
  130. $db=self::cdb();
  131. $db->setTableName($table);
  132. return $db->select($where , $fields , $limit, $order , $group);
  133. }
  134. /**
  135. * 获取单条记录查询
  136. * @param $table 表名称
  137. * @param array $where查询条件语句
  138. * 字符串,请按照格式 :
  139. * 字符串 [例2:array('catid=:catid AND time>=:time ',array(':catid'=>10,':time'=>'2012-02-10')) ]
  140. * 数组时 [例: array('name'=>'lanmps','password'=>'123456')]
  141. * @param string $fields 需要查询的字段值[例`name`,`gender`,`birthday`]
  142. * @param string $order 排序方式 [默认按数据库默认方式排序]
  143. * @param string $group 分组方式 [默认为空]
  144. * @return array/null数据查询结果集,如果不存在,则返回空
  145. */
  146. public static function getOne($table,$where,$fields = '*', $order = '', $group = '') {
  147. $db=self::cdb();
  148. $db->setTableName($table);
  149. return $db->get_one($where , $fields,$order, $group);
  150. }
  151. /**
  152. * 查询多条数据并分页
  153. * @param $table 表名称
  154. * @param $where 查询条件
  155. * 字符串,请按照格式 :
  156. * 字符串 [例1:" id=1 and time>$time " ]
  157. * 字符串 [例2:array('catid=:catid AND time>=:time ',array(':catid'=>10,':time'=>'2012-02-10')) ]
  158. * 数组时 [例: array('name'=>'lanmps','password'=>'123456')]
  159. * @param $fields字段 *,id
  160. * @param $order 排序 id desc ,orderlist asc
  161. * @param $page 页码 1
  162. * @param $pagesize 每页条数
  163. * @return array('data'=>数据,'count'=>记录总数)
  164. */
  165. public static function listInfo($table,$where = '',$fields='*', $order = '', $page = 1, $pagesize = 20) {
  166. $db=self::cdb();
  167. $db->setTableName($table);
  168. $d=$db->listinfo($where,$fields, $order, $page, $pagesize);
  169. return array('data'=>$d,'count'=>self::$db->number);
  170. }
  171. /**第一个参数值
  172. * @param $sql
  173. * @return mixed
  174. */
  175. public static function resultFirst($sql){
  176. $db=self::cdb();
  177. return $db->resultFirst($sql);
  178. }
  179. }
复制代码

调用方法:

DB::insert('test',array('name'=>'test'));


陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
繼續使用PHP:耐力的原因繼續使用PHP:耐力的原因Apr 19, 2025 am 12:23 AM

PHP仍然流行的原因是其易用性、靈活性和強大的生態系統。 1)易用性和簡單語法使其成為初學者的首選。 2)與web開發緊密結合,處理HTTP請求和數據庫交互出色。 3)龐大的生態系統提供了豐富的工具和庫。 4)活躍的社區和開源性質使其適應新需求和技術趨勢。

PHP和Python:探索他們的相似性和差異PHP和Python:探索他們的相似性和差異Apr 19, 2025 am 12:21 AM

PHP和Python都是高層次的編程語言,廣泛應用於Web開發、數據處理和自動化任務。 1.PHP常用於構建動態網站和內容管理系統,而Python常用於構建Web框架和數據科學。 2.PHP使用echo輸出內容,Python使用print。 3.兩者都支持面向對象編程,但語法和關鍵字不同。 4.PHP支持弱類型轉換,Python則更嚴格。 5.PHP性能優化包括使用OPcache和異步編程,Python則使用cProfile和異步編程。

PHP和Python:解釋了不同的範例PHP和Python:解釋了不同的範例Apr 18, 2025 am 12:26 AM

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

PHP和Python:深入了解他們的歷史PHP和Python:深入了解他們的歷史Apr 18, 2025 am 12:25 AM

PHP起源於1994年,由RasmusLerdorf開發,最初用於跟踪網站訪問者,逐漸演變為服務器端腳本語言,廣泛應用於網頁開發。 Python由GuidovanRossum於1980年代末開發,1991年首次發布,強調代碼可讀性和簡潔性,適用於科學計算、數據分析等領域。

在PHP和Python之間進行選擇:指南在PHP和Python之間進行選擇:指南Apr 18, 2025 am 12:24 AM

PHP適合網頁開發和快速原型開發,Python適用於數據科學和機器學習。 1.PHP用於動態網頁開發,語法簡單,適合快速開發。 2.Python語法簡潔,適用於多領域,庫生態系統強大。

PHP和框架:現代化語言PHP和框架:現代化語言Apr 18, 2025 am 12:14 AM

PHP在現代化進程中仍然重要,因為它支持大量網站和應用,並通過框架適應開發需求。 1.PHP7提升了性能並引入了新功能。 2.現代框架如Laravel、Symfony和CodeIgniter簡化開發,提高代碼質量。 3.性能優化和最佳實踐進一步提升應用效率。

PHP的影響:網絡開發及以後PHP的影響:網絡開發及以後Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP類型提示如何起作用,包括標量類型,返回類型,聯合類型和無效類型?PHP類型提示如何起作用,包括標量類型,返回類型,聯合類型和無效類型?Apr 17, 2025 am 12:25 AM

PHP類型提示提升代碼質量和可讀性。 1)標量類型提示:自PHP7.0起,允許在函數參數中指定基本數據類型,如int、float等。 2)返回類型提示:確保函數返回值類型的一致性。 3)聯合類型提示:自PHP8.0起,允許在函數參數或返回值中指定多個類型。 4)可空類型提示:允許包含null值,處理可能返回空值的函數。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。