当前位置:Gxlcms > 数据库问题 > 封装自己DB

封装自己DB

时间:2021-07-01 10:21:17 帮助过:24人阅读

DB.class.php 

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: brady.wang
  5. * Date: 2017/11/10
  6. * Time: 18:00
  7. */
  8. //封装一个DB类,用来专门操作数据库,以后凡是对数据库的操作,都由DB类的对象来实现
  9. class DB{
  10. //属性
  11. private $host;
  12. private $port;
  13. private $user;
  14. private $pass;
  15. private $dbname;
  16. private $charset;
  17. private $prefix; //表前缀
  18. private $link; //连接资源(连接数据库,一般会返回一个资源,所以需要定义一个link属性)
  19. //构造方法(作用:为了初始化对象的属性),会被自动调用
  20. /*
  21. * @param1 array $arr,默认为空,里面是一个关联数组,里面有7个元素
  22. * array(‘host‘ => ‘localhost‘,‘port‘ => ‘3306‘);
  23. */
  24. public function __construct($arr = array()){
  25. //初始化
  26. $this->host = isset($arr[‘host‘]) ? $arr[‘host‘] : ‘localhost‘;//先判断是否有自己的host,如果有就用自己的host,否则就使用默认的localhost
  27. $this->port = isset($arr[‘port‘]) ? $arr[‘port‘] : ‘3306‘;
  28. $this->user = isset($arr[‘user‘]) ? $arr[‘user‘] : ‘root‘;
  29. $this->pass = isset($arr[‘pass‘]) ? $arr[‘pass‘] : ‘root‘;
  30. $this->dbname = isset($arr[‘dbname‘]) ? $arr[‘dbname‘] : ‘test‘;
  31. $this->charset = isset($arr[‘charset‘]) ? $arr[‘charset‘] : ‘utf8‘;
  32. $this->prefix = isset($arr[‘prefix‘]) ? $arr[‘prefix‘] : ‘‘;
  33. //连接数据库(类是要操作数据库,因此要连接数据库)
  34. $this->connect();
  35. //设置字符集
  36. $this->setCharset();
  37. //选择数据库
  38. $this->setDbname();
  39. }
  40. /*
  41. * 连接数据库
  42. */
  43. private function connect(){
  44. //mysql扩展连接
  45. $this->link = mysql_connect($this->host . ‘:‘ . $this->port,$this->user,$this->pass);
  46. //判断结果
  47. if(!$this->link){
  48. //结果出错了
  49. //暴力处理,如果是真实线上项目(生产环境)必须写入到日志文件
  50. echo ‘数据库连接错误:<br/>‘;
  51. echo ‘错误编号‘ . mysql_errno() . ‘<br/>‘;
  52. echo ‘错误内容‘ . mysql_error() . ‘<br/>‘;
  53. exit;
  54. }
  55. }
  56. /*
  57. * 设置字符集
  58. */
  59. private function setCharset(){
  60. //设置
  61. $this->db_query("set names {$this->charset}");
  62. }
  63. /*
  64. * 选择数据库
  65. */
  66. private function setDbname(){
  67. $this->db_query("use {$this->dbname}");
  68. }
  69. /*
  70. * 增加数据
  71. * @param1 string $sql,要执行的插入语句
  72. * @return boolean,成功返回是自动增长的ID,失败返回FALSE
  73. */
  74. public function insert($sql){
  75. //发送数据
  76. $this->db_query($sql);
  77. //成功返回自增ID
  78. return mysql_affected_rows() ? mysql_insert_id() : FALSE;
  79. }
  80. /*
  81. * 删除数据
  82. * @param1 string $sql,要执行的删除语句
  83. * @return Boolean,成功返回受影响的行数,失败返回FALSE
  84. */
  85. public function delete($sql){
  86. //发送SQL
  87. $this->db_query($sql);
  88. //判断结果
  89. return mysql_affected_rows() ? mysql_affected_rows() : FALSE;
  90. }
  91. /*
  92. * 更新数据
  93. * @param1 string $sql,要执行的更新语句
  94. * @return Boolean,成功返回受影响的行数,失败返回FALSE
  95. */
  96. public function update($sql){
  97. //发送SQL
  98. $this->db_query($sql);
  99. //判断结果
  100. return mysql_affected_rows() ? mysql_affected_rows() : FALSE;
  101. }
  102. /*
  103. * 查询:查询一条记录
  104. * @param1 string $sql,要查询的SQL语句
  105. * @return mixed,成功返回一个数组,失败返回FALSE
  106. */
  107. public function get_row($sql){
  108. //发送SQL
  109. $res = $this->db_query($sql);
  110. //判断返回
  111. return mysql_num_rows($res) ? mysql_fetch_assoc($res) : FALSE;
  112. }
  113. /*
  114. * 查询:查询多条记录
  115. * @param1 string $sql,要查询的SQL语句
  116. * @return mixed,成功返回一个二维数组,失败返回FALSE
  117. */
  118. public function get_all($sql){
  119. //发送SQL
  120. $res = $this->db_query($sql);
  121. //判断返回
  122. if(mysql_num_rows($res)){
  123. //循环遍历
  124. $list = array();
  125. //遍历
  126. while($row = mysql_fetch_assoc($res)){
  127. $list[] = $row;
  128. }
  129. //返回
  130. return $list;
  131. }
  132. //返回FALSE
  133. return FALSE;
  134. }
  135. /*
  136. * mysql_query错误处理
  137. * @param1 string $sql,需要执行的SQL语句
  138. * @return mixed,只要语句不出错,全部返回
  139. */
  140. private function db_query($sql){
  141. //发送SQL
  142. $res = mysql_query($sql);
  143. //判断结果
  144. if(!$res){
  145. //结果出错了
  146. //暴力处理,如果是真实线上项目(生产环境)必须写入到日志文件
  147. echo ‘语句出现错误:<br/>‘;
  148. echo ‘错误编号‘ . mysql_errno() . ‘<br/>‘;
  149. echo ‘错误内容‘ . mysql_error() . ‘<br/>‘;
  150. exit;
  151. }
  152. //没有错误
  153. return $res;
  154. }
  155. //__sleep方法
  156. public function __sleep(){
  157. //返回需要保存的属性的数组
  158. return array(‘host‘,‘port‘,‘user‘,‘pass‘,‘dbname‘,‘charset‘,‘prefix‘);
  159. }
  160. //__wakeup方法
  161. public function __wakeup(){
  162. //连接资源
  163. $this->connect();
  164. //设置字符集和选中数据库
  165. $this->setCharset();
  166. $this->setDbname();
  167. }
  168. /*
  169. * 获取完整的表名
  170. */
  171. protected function get_table_name(){
  172. //完整表名:前缀+表名
  173. return $this->prefix . $this->table;
  174. }
  175. }
  176. //这个DB类,一般不写析构(不释放资源)

  test.php

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: brady.wang
  5. * Date: 2017/11/10
  6. * Time: 18:04
  7. */
  8. //使用DB类的对象来访问数据库
  9. //先加载类文件
  10. //include_once ‘DB.class.php‘;
  11. //如果想使用其他类,又需要加载(所以使用魔术函数__autoload来实现类的自动加载)
  12. //显示的写出魔术函数__autoload
  13. //参数:需要加载的类的名字
  14. function __autoload($a){
  15. //将对应的类文件加载进来
  16. if(is_file("$a.class.php")){
  17. include_once "$a.class.php";
  18. }
  19. }
  20. //实例化
  21. $db = new DB(array(‘host‘ => ‘192.168.33.30‘));
  22. $sql = "select * from user where id = 1";
  23. $res = $db->get_row($sql);
  24. function dump($arr)
  25. {
  26. echo "<pre>";
  27. print_r($arr);
  28. echo "</pre>";
  29. }
  30. dump($res);

  

封装自己DB

标签:作用   个数   编号   func   boolean   nbsp   失败   prot   cal   

人气教程排行