php实现Mysql简易操作类

这个PHP实现的mysql的操作类完整版已经使用过了,而这个简化版是经过修改完整版后的简化版,适用在一般的 PHP 应用中,功能上可以实现基本的增删改查的操作,以及打印 MYSQL 错误,自我感觉不错,如果网站应用不是很强大,应用这个 MYSQL 的操作类已经足够了。

自己封装的Mysql简易操作类,已塞在Ben框架中,基于PDO来写的,代码风格上有些无厘头。。。

mysql.class.php

  1. <?php
  2. class mysql extends PDO{
  3. public $server;
  4. public $database;
  5. public $user;
  6. public $password;
  7. public $sql;
  8. public function __construct($server,$database,$user,$password,$port=3306){
  9. $this->server = $server;
  10. $this->database = $database;
  11. $this->user = $user;
  12. $this->password = $password;
  13. parent::__construct("mysql:host=$server;port=$port;dbname=$database",$user,$password);
  14. $this->query('SET NAMES utf8');
  15. }
  16. public function drop($table){
  17. $sql = 'DROP TABLE '.$table.';';
  18. $re = $this->query($sql);
  19. if($re){
  20. return true;
  21. }else{
  22. return false;
  23. }
  24. }
  25. public function insert($table,$name,$value=null){
  26. $sql = "INSERT INTO ".$table.'(';
  27. if($value == null){
  28. $arrname = array_keys($name);
  29. $arrvalue = array_values($name);
  30. }else{
  31. $arrname = explode('|', $name);
  32. $arrvalue = explode('|', $value);
  33. }
  34. for($i=0;$i<count($arrname);$i++){
  35. if($i==count($arrname)-1){
  36. $sql = $sql.$arrname[$i];
  37. }else{
  38. $sql = $sql.$arrname[$i].",";
  39. }
  40. }
  41. $sql = $sql.")VALUES(";
  42. for($i=0;$i<count($arrvalue);$i++){
  43. if($i==count($arrvalue)-1){
  44. $sql = $sql."'".$arrvalue[$i]."'";
  45. }else{
  46. $sql = $sql."'".$arrvalue[$i]."',";
  47. }
  48. }
  49. $sql .=");";
  50. $re = $this->query($sql);
  51. if($re){
  52. return true;
  53. }else{
  54. return false;
  55. }
  56. }
  57. public function delete($table,$Conditionsname,$Conditionsvalue=null){
  58. if($Conditionsvalue!=null){
  59. $sql = "DELETE FROM ".$table." WHERE ".$Conditionsname."='".$Conditionsvalue."';";
  60. }else{
  61. $sql = "DELETE FROM ".$table." WHERE ";
  62. $arrname = array_keys($Conditionsname);
  63. $arrvalue = array_values($Conditionsname);
  64. for($i=0;$i<count($arrname);$i++){
  65. if($i==count($arrname)-1){
  66. $sql.=$arrname[$i].'='."'".$arrvalue[$i]."'";
  67. }else{
  68. $sql.=$arrname[$i].'='."'".$arrvalue[$i]."',";
  69. }
  70. }
  71. $sql.=';';
  72. }
  73. $re = $this->query($sql);
  74. if($re){
  75. return true;
  76. }else{
  77. return false;
  78. }
  79. }
  80. public function select($table,$name,$Conditionsname,$Conditionsvalue=null){
  81. if($Conditionsvalue!=null){
  82. $sql = "SELECT ".$name." FROM ".$table." WHERE ".$Conditionsname."='".$Conditionsvalue."';";
  83. }else{
  84. $sql = "SELECT ".$name." FROM ".$table." WHERE ";
  85. $arrname = array_keys($Conditionsname);
  86. $arrvalue = array_values($Conditionsname);
  87. for($i=0;$i<count($arrname);$i++){
  88. if($i==count($arrname)-1){
  89. $sql.=$arrname[$i].'='."'".$arrvalue[$i]."'";
  90. }else{
  91. $sql.=$arrname[$i].'='."'".$arrvalue[$i]."' and ";
  92. }
  93. }
  94. $sql.=';';
  95. }
  96. $re = $this->query($sql);
  97. $row = $re->fetch();
  98. return $row[$name];
  99. }
  100. public function update($table,$name,$value,$Conditionsname,$Conditionsvalue=null){
  101. if($Conditionsvalue!=null){
  102. $sql = "UPDATE ".$table." SET ".$name."= '".$value."' WHERE ".$Conditionsname."='".$Conditionsvalue."';";
  103. }else{
  104. $sql = "UPDATE ".$table." SET ".$name."= '".$value."' WHERE ";
  105. $arrname = array_keys($Conditionsname);
  106. $arrvalue = array_values($Conditionsname);
  107. for($i=0;$i<count($arrname);$i++){
  108. if($i==count($arrname)-1){
  109. $sql.=$arrname[$i].'='."'".$arrvalue[$i]."'";
  110. }else{
  111. $sql.=$arrname[$i].'='."'".$arrvalue[$i]."' and ";
  112. }
  113. }
  114. $sql.=';';
  115. }
  116. $re = $this->query($sql);
  117. if($re){
  118. return true;
  119. }else{
  120. return false;
  121. }
  122. }
  123. public function group($table,$name){
  124. $sql = "SELECT ".$name." FROM ".$table.";";
  125. $return = array();
  126. $re = $this->query($sql);
  127. while($row = $re->fetch(PDO::FETCH_ASSOC)){
  128. array_push($return,$row[$name]);
  129. }
  130. return $return;
  131. }
  132. public function fetchall($sql){
  133. $return = array();
  134. $re = $this->query($sql);
  135. while($row = $re->fetch(PDO::FETCH_ASSOC)){
  136. array_push($return,$row);
  137. }
  138. return $return;
  139. }
  140. }