当前位置:Gxlcms > PHP教程 > php中关于mysql数据库操作类的实例解析

php中关于mysql数据库操作类的实例解析

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

本白演示的代码属于较为简单的数据库封装类,较适合初学,需要的朋友可以参考下

接着稍微说说整体的思路。整个类的封装,包含一个连接数据库的私有属性$conn和若干操作函数。$conn在对象实例化的时候,由构造函数处理传入的参数后返回一个资源型的连接句柄。而后即可通过调用该实例化的对象的相应方法对数据库进行增删查改的操作。

talk less and show code:


  1. <?php
  2. /**
  3. *以下代码用于数据库操作类的封装
  4. *
  5. * @author rex<rex.sp.li@aliyun.com>
  6. * @version 1.0
  7. * @since 2015
  8. */
  9. class Mysql{
  10. //数据库连接返回值
  11. private $conn;
  12. /**
  13. * [构造函数,返回值给$conn]
  14. * @param [string] $hostname [主机名]
  15. * @param [string] $username[用户名]
  16. * @param [string] $password[密码]
  17. * @param [string] $dbname[数据库名]
  18. * @param [string] $charset[字符集]
  19. * @return [null]
  20. */
  21. function __construct($hostname,$username,$password,$dbname,$charset='utf8'){
  22.   $conn = @mysql_connect($hostname,$username,$password);
  23.   if(!$conn){
  24.     echo '连接失败,请联系管理员';
  25.     exit;
  26.   }
  27.   $this->conn = $conn;
  28.   $res = mysql_select_db($dbname);
  29.   if(!$res){
  30.   echo '连接失败,请联系管理员';
  31.   exit;
  32.   }
  33.   mysql_set_charset($charset);
  34. }
  35. function __destruct(){
  36.   mysql_close();
  37. }
  38. /**
  39. * [getAll 获取所有信息]
  40. * @param [string] $sql [sql语句]
  41. * @return [array] [返回二维数组]
  42. */
  43. function getAll($sql){
  44.   $result = mysql_query($sql,$this->conn);
  45.   $data = array();
  46.   if($result && mysql_num_rows($result)>0){
  47.     while($row = mysql_fetch_assoc($result)){
  48.     $data[] = $row;
  49.     }
  50.   }
  51.   return $data;
  52. }
  53. /**
  54. * [getOne 获取单条数据]
  55. * @param [string] $sql [sql语句]
  56. * @return [array] [返回一维数组]
  57. */
  58. function getOne($sql){
  59.   $result = mysql_query($sql,$this->conn);
  60.   $data = array();
  61.   if($result && mysql_num_rows($result)>0){
  62.     $data = mysql_fetch_assoc($result);
  63.   }
  64.   return $data;
  65. }
  66. /**
  67. * [getOne 获取单条数据]
  68. * @param [string] $table [表名]
  69. * @param [string] $data [由字段名当键,属性当键值的一维数组]
  70. * @return [type] [返回false或者插入数据的id]
  71. */
  72. function insert($table,$data){
  73.   $str = '';
  74.   $str .="INSERT INTO `$table` ";
  75.   $str .="(`".implode("`,`",array_keys($data))."`) ";
  76.   $str .=" VALUES ";
  77.   $str .= "('".implode("','",$data)."')";
  78.   $res = mysql_query($str,$this->conn);
  79.   if($res && mysql_affected_rows()>0){
  80.       return mysql_insert_id();
  81.   }else{
  82.     return false;
  83.   }
  84. }
  85. /**
  86. * [update 更新数据库]
  87. * @param [string] $table [表名]
  88. * @param [array] $data [更新的数据,由字段名当键,属性当键值的一维数组]
  89. * @param [string] $where [条件,‘字段名'=‘字段属性']
  90. * @return [type] [更新成功返回影响的行数,更新失败返回false]
  91. */
  92. function update($table,$data,$where){
  93.   $sql = 'UPDATE '.$table.' SET ';
  94.   foreach($data as $key => $value){
  95.   $sql .= "`{$key}`='{$value}',";
  96.   }
  97.   $sql = rtrim($sql,',');
  98.   $sql .= " WHERE $where";
  99.   $res = mysql_query($sql,$this->conn);
  100.   if($res && mysql_affected_rows()){
  101.     return mysql_affected_rows();
  102.   }else{
  103.   return false;
  104.   }
  105. }
  106. /**
  107. * [delete 删除数据]
  108. * @param [string] $table [表名]
  109. * @param [string] $where [条件,‘字段名'=‘字段属性']
  110. * @return [type] [成功返回影响的行数,失败返回false]
  111. */
  112. function del($table,$where){
  113.   $sql = "DELETE FROM `{$table}` WHERE {$where}";
  114.   $res = mysql_query($sql,$this->conn);
  115.   if($res && mysql_affected_rows()){
  116.     return mysql_affected_rows();
  117.   }else{
  118.   return false;
  119.   }
  120. }
  121. }

实例化类:


  1. <?php
  2. //包含数据库操作类文件
  3. include 'mysql.class.php';
  4. //设置传入参数
  5. $hostname='localhost';
  6. $username='root';
  7. $password='123456';
  8. $dbname='aisi';
  9. $charset = 'utf8';
  10. //实例化对象
  11. $db = new Mysql($hostname,$username,$password,$dbname);
  12. //获取一条数据
  13. $sql = "SELECT count(as_article_id) as count FROM as_article where as_article_type_id=1";
  14. $count = $db->getOne($sql);
  15. //获取多条数据
  16. $sql = "SELECT * FROM as_article where as_article_type_id=1 order by as_article_addtime desc limit $start,$limit";
  17. $service = $db->getAll($sql);
  18. //插入数据
  19. $arr = array(
  20. 'as_article_title'=>'数据库操作类',
  21. 'as_article_author'=>'rex',
  22. );
  23. $res = $db->insert('as_article',$arr);
  24. //更新数据
  25. $arr = array(
  26. 'as_article_title'=>'实例化对象',
  27. 'as_article_author'=>'Lee',
  28. );
  29. $where = "as_article_id=1";
  30. $res = $db->update('as_article',$arr,$where);
  31. //删除数据
  32. $where = "as_article_id=1";
  33. $res = $db->del('as_article',$where);
  34. ?>

演示完代码,大概说几句。

  getOne方法传入$sql的sql语句用于查询单条数据并返回一维数组;getAll方法同样传入sql语句,用于查询多条数据,并返回二维数组;insert方法传入表名和关联数组,返回boolen型或者插入数据对应索引;update方法传入表名、关联数组和条件,返回boolen或者影响的行数;del方法传入表名和条件,返回boolen型。

  that's all,but not the all.有兴趣的朋友可以把getOne和getAll直接传入sql语句作为参数的方式再优化一下。

以上就是php中关于mysql数据库操作类的实例解析的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行