当前位置:Gxlcms > PHP教程 > 一个数据库操作PHP类

一个数据库操作PHP类

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

  1. /*
  2. * Author 墨龙
  3. * Time 2010年12月2日 15:50:35
  4. */
  5. $db = new mysql($db_host,$db_user,$db_password,$db_table,$db_conn,$pre,$coding);
  6. class mysql{
  7. private $db_host;
  8. private $db_user;
  9. private $db_password;
  10. private $db_table;
  11. private $db_conn; //数据库连接标识;
  12. private $result; //执行query命令的结果资源标识
  13. private $sql; //sql执行语句
  14. private $pre; //数据库表前缀
  15. private $coding; //数据库编码,GBK,UTF8,gb2312
  16. function __construct($db_host,$db_user,$db_password,$db_table,$db_conn,$pre,$coding){
  17. $this->db_host = $db_host;
  18. $this->db_user = $db_user;
  19. $this->db_password = $db_password;
  20. $this->db_table = $db_table;
  21. $this->db_conn = $db_conn;
  22. $this->pre = $pre;
  23. $this->coding = $coding;
  24. $this->connect();
  25. }
  26. function connect(){
  27. $this->db_conn = @mysql_connect($this->db_host,$this->db_user,$this->db_password) or die($this->show_error("数据库链接错误,请检查数据库链接配置!"));
  28. if(!mysql_select_db($this->db_table,$this->db_conn)){
  29. echo "没有找到数据表:".$this->db_table;
  30. }
  31. mysql_select_db($this->db_table,$this->db_conn);
  32. $this->query("SET NAMES $this->coding");
  33. }
  34. /*执行SQL语句的函数*/
  35. function query($sql){
  36. if(emptyempty($sql)){
  37. $this->show_error("你的sql语句不能为空!");
  38. }else{
  39. $this->sql = $sql;
  40. }
  41. $result = mysql_query($this->sql,$this->db_conn);
  42. return $this->result = $result;
  43. }
  44. /*创建添加新的数据库*/
  45. public function create_database($database_name){
  46. $database=$database_name;
  47. $sqlDatabase = 'create database '.$database;
  48. return $this->query($sqlDatabase);
  49. }
  50. // 根据select查询结果计算结果集条数
  51. public function db_num_rows(){
  52. if($this->result==null){
  53. if($this->show_error){
  54. $this->show_error("sql语句错误!");
  55. }
  56. }else{
  57. return mysql_num_rows($this->result);
  58. }
  59. }
  60. /*查询服务器所有数据库*/
  61. //将系统数据库与用户数据库分开,更直观的显示?
  62. public function show_databases(){
  63. $this->query("show databases");
  64. echo "现有数据库:".$amount =$this->db_num_rows($rs);
  65. echo "
    ";
  66. $i=1;
  67. while($row = $this->fetch_array($rs)){
  68. echo "$i $row[Database]";
  69. echo "
    ";
  70. $i++;
  71. }
  72. }
  73. //以数组形式返回主机中所有数据库名
  74. public function databases()
  75. {
  76. $rsPtr=mysql_list_dbs($this->db_conn);
  77. $i=0;
  78. $cnt=mysql_num_rows($rsPtr);
  79. while($i<$cnt)
  80. {
  81. $rs[]=mysql_db_name($rsPtr,$i);
  82. $i++;
  83. }
  84. return print_r($rs);
  85. }
  86. /*查询数据库下所有的表*/
  87. function show_tables($database_name){
  88. $this->query("show tables");
  89. echo "现有数据库:".$amount = $this->db_num_rows($rs);
  90. echo "
    ";
  91. $i=1;
  92. while($row = $this->fetch_array($rs)){
  93. $columnName="Tables_in_".$database_name;
  94. echo "$i $row[$columnName]";
  95. echo "
    ";
  96. $i++;
  97. }
  98. }
  99. /*
  100. mysql_fetch_row() array $row[0],$row[1],$row[2]
  101. mysql_fetch_array() array $row[0] 或 $row[id]
  102. mysql_fetch_assoc() array 用$row->content 字段大小写敏感
  103. mysql_fetch_object() object 用$row[id],$row[content] 字段大小写敏感
  104. */
  105. /*取得记录集,获取数组-索引和关联,使用$row['content'] */
  106. public function fetch_array()
  107. {
  108. return @mysql_fetch_array($this->result);
  109. }
  110. //获取关联数组,使用$row['字段名']
  111. public function fetch_ass()
  112. {
  113. return @mysql_fetch_assoc($this->result);
  114. }
  115. //获取数字索引数组,使用$row[0],$row[1],$row[2]
  116. public function fetch_row()
  117. {
  118. return @mysql_fetch_row($this->result);
  119. }
  120. //获取对象数组,使用$row->content
  121. public function fetch_Object()
  122. {
  123. return @mysql_fetch_object($this->result);
  124. }
  125. //简化查询select
  126. public function findall($table){
  127. $table = $this->fulltablename($table);
  128. $this->query("select * from $table");
  129. }
  130. public function select($table,$columnName,$condition){
  131. $table = $this->fulltablename($table);
  132. if(emptyempty($columnName)){
  133. $columnName = "*";
  134. }
  135. $this->query("SELECT $columnName FROM $table $condition");
  136. }
  137. //简化的insert
  138. function insert($table,$arr){
  139. $table = $this->fulltablename($table);
  140. $sql = "INSERT INTO $table ";
  141. if(!is_array($arr)){
  142. $this->show_error("请输入参数数组!");
  143. }else{
  144. $k = "";
  145. $v = "";
  146. foreach($arr as $key => $value){
  147. $k .= "`$key`,";
  148. $v .= "'".$value."',";
  149. }
  150. }
  151. $sql = $sql." (".substr($k,0,-1).") VALUES (".substr($v,0,-1).")";
  152. $this->query($sql);
  153. }
  154. //简化的update
  155. function update($table,$arr,$where){
  156. $table = $this->fulltablename($table);
  157. $sql = "UPDATE $table SET ";
  158. if(!is_array($arr)){
  159. $this->show_error("请输入参数数组!");
  160. }else{
  161. foreach($arr as $key => $value){
  162. $sql .= " `".$key."` = '".$value."' ,";
  163. }
  164. }
  165. $sql = substr($sql,0,-1)." where ".$where;
  166. return $this->query($sql);
  167. }
  168. //简化的delete
  169. function delete($table,$where = ""){
  170. $table = $this->fulltablename($table);
  171. if(emptyempty($where)){
  172. $this->show_error("条件不能为空!");
  173. }else{
  174. $where = " where ".$where;
  175. }
  176. $sql = "DELETE FROM $table ".$where;
  177. //echo $sql;
  178. return $this->query($sql);
  179. }
  180. //取得上一步 INSERT 操作产生的 ID
  181. public function insert_id(){
  182. return mysql_insert_id();
  183. }
  184. //加上前缀的数据表
  185. public function fulltablename($table){
  186. return $table = $this->pre.$table;
  187. }
  188. //查询字段数量
  189. public function num_fields($table){
  190. $table = $this->fulltablename($table);
  191. $this->query("select * from $table");
  192. echo "
    ";
  193. echo "字段数:".$total = mysql_num_fields($this->result);
  194. echo "
    ";  
  195. for ($i=0; $i<$total; $i++){
  196. print_r(mysql_fetch_field($this->result,$i) );
  197. }
  198. echo "
  199. ";
  200. echo "
    ";
  201. }
  202. //取得 MySQL 服务器信息
  203. public function mysql_server($num=''){
  204. switch ($num){
  205. case 1 :
  206. return mysql_get_server_info(); //MySQL 服务器信息
  207. break;
  208. case 2 :
  209. return mysql_get_host_info(); //取得 MySQL 主机信息
  210. break;
  211. case 3 :
  212. return mysql_get_client_info(); //取得 MySQL 客户端信息
  213. break;
  214. case 4 :
  215. return mysql_get_proto_info(); //取得 MySQL 协议信息
  216. break;
  217. default:
  218. return mysql_get_client_info(); //默认取得mysql版本信息
  219. }
  220. }
  221. //析构函数,自动关闭数据库,垃圾回收机制
  222. /*public function __destruct()
  223. {
  224. if(!empty($this->result)){
  225. $this->free();
  226. }
  227. mysql_close($this->$db_conn);
  228. }*/
  229. /*获得客户端真实的IP地址*/
  230. function getip(){
  231. if(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
  232. {
  233. $ip = getenv("HTTP_CLIENT_IP");
  234. }
  235. else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")){
  236. $ip = getenv("HTTP_X_FORWARDED_FOR");
  237. }
  238. else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
  239. {
  240. $ip = getenv("REMOTE_ADDR");
  241. }
  242. else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")){
  243. $ip = $_SERVER['REMOTE_ADDR'];
  244. }
  245. else{
  246. $ip = "unknown";
  247. }
  248. return($ip);
  249. }
  250. function show_error($str){
  251. echo "";
  252. }
  253. }
  254. ?>

PHP

人气教程排行