PHP封装了一个DB数据库 mysql 的类

WBOY
发布: 2016-07-23 08:54:58
原创
961 人浏览过
  1. // 配置数据库
  2. define('DB_HOST', '127.0.0.1'); //服务器地址
  3. define('DB_USER', 'root'); //用户名
  4. define('DB_PASS', ''); //密码
  5. define('DB_DATABASENAME', 'fenxiao'); //数据库
  6. class Dbmysql
  7. {
  8. /*
  9. *变量
  10. **/
  11. private $tablename=""; //表名
  12. private $fieldname="*";
  13. private $conn;
  14. private $where;
  15. private $sql;
  16. function __construct($tablename)
  17. {
  18. //生成一个连接
  19. $this->conn = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("connect failed" . mysql_error());
  20. //选择数据库
  21. mysql_select_db(DB_DATABASENAME, $this->conn);
  22. //设置编码格式
  23. mysql_query("SET NAMES utf8");
  24. //var_dump($conn);
  25. $this->tablename=$tablename;
  26. }
  27. //设置sql语句
  28. private function setsql($sql)
  29. {
  30. $this->sql=$sql;
  31. }
  32. //设置条件语句
  33. public function where($where)
  34. {
  35. $this->where=" where ".$where;
  36. return $this;
  37. }
  38. //按指定字段
  39. public function field($keyword)
  40. {
  41. $this->fieldname=$keyword;
  42. return $this;
  43. }
  44. //设置连接查询表
  45. public function table($table1,$table2,$field,$bool)
  46. {
  47. $this->tablename="$table1 LEFT JOIN $table2 ON $table1.$field$bool$table2.$field";
  48. //print_r($this->tablename);
  49. return $this;
  50. }
  51. //设置多表查询
  52. public function addtable($table1,$table2,$field,$bool)
  53. {
  54. $this->tablename.=" LEFT JOIN $table2 ON $table1.$field$bool$table2.$field";
  55. //print_r($this->tablename);
  56. return $this;
  57. }
  58. //设置连接查询表
  59. ##SELECT * FROM 【wx_order LEFT JOIN wx_shopcar ON wx_shopcar.oid=wx_order.oid and wx_order.uid=wx_shopcar.uid LEFT JOIN wx_goods ON wx_shopcar.gid=wx_goods.gid】 WHERE wx_order.oid=1 and wx_order.uid=3
  60. public function settable($sql)
  61. {
  62. $this->tablename=$sql;
  63. //print_r($this->tablename);
  64. return $this;
  65. }
  66. //查询所有数据库 以数组形式输出
  67. public function select()
  68. {
  69. /**
  70. * 查询数据库中所有的数据
  71. **/
  72. $arr=array();
  73. //执行sql语句
  74. $result = mysql_query("select ".$this->fieldname." from ".$this->tablename.$this->where, $this->conn);
  75. while ($row = mysql_fetch_assoc($result)) {
  76. array_push($arr, $row);
  77. }
  78. return $arr;
  79. }
  80. //搜索指定字段数据
  81. public function find()
  82. {
  83. //执行sql语句
  84. $result = mysql_query("select ".$this->fieldname." from ".$this->tablename.$this->where, $this->conn);
  85. $result = mysql_fetch_assoc($result);
  86. return $result;
  87. }
  88. //增加数据到数据库
  89. public function add($data)
  90. {
  91. $keysql='';
  92. $valuesql='';
  93. foreach ($data as $key => $value) {
  94. $keysql.=",`$key`";
  95. $valuesql.=",'$value'";
  96. }
  97. $keysql=substr($keysql, 1);
  98. $valuesql=substr($valuesql, 1);
  99. $result=mysql_query("insert into `".$this->tablename."` ($keysql) VALUES($valuesql)");
  100. $id=mysql_insert_id();
  101. //print_r("insert into `".$this->tablename."` ($keysql) VALUES($valuesql)");
  102. return $id;
  103. }
  104. //修改数据库的内容
  105. public function save($data)
  106. {
  107. $keysql='';
  108. $valuesql='';
  109. foreach ($data as $key => $value) {
  110. $keysql.=",`$key`='$value'";
  111. }
  112. $keysql=substr($keysql, 1);
  113. //print_r($keysql);
  114. //echo "
    ";
  115. $result=mysql_query("UPDATE `".$this->tablename."` SET ".$keysql.$this->where);
  116. //print_r("UPDATE `".$this->tablename."` SET ".$keysql.$this->where);
  117. return $result;
  118. }
  119. ##删除数据
  120. public function delete()
  121. {
  122. $result=mysql_query("DELETE FROM $this->tablename $this->where");
  123. //print_r("DELETE FROM $this->tablename $this->where");
  124. return $result;
  125. }
  126. }
  127. /**
  128. * mysql_fetch_row: 返回单列的各字段 [0]=>"111"
  129. * mysql_fetch_field: 取得字段信息。[0]=> ['name']=> object
  130. * mysql_fetch_array 返回数组资料。 [0]=>"asasds" ['name']=>
  131. */
  132. ?>
复制代码

装了, PHP, mysql


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板