当前位置:首页 > Mysql教程 > 列表

Mysql数据库操作

发布:smiling 来源: PHP粉丝网  添加日期:2014-06-04 09:24:52 浏览: 评论:0 
  1. <?php 
  2. class opmysql{  
  3. private $host = 'localhost'; //服务器地址  
  4. private $name = 'root'; //登录账号  
  5. private $pwd = ''; //登录密码  
  6. private $dbase = 'a0606123620'; //数据库名称  
  7. private $conn = ''; //数据库链接资源  
  8. private $result = ''; //结果集  
  9. private $msg = ''; //返回结果  
  10. private $fields; //返回字段  
  11. private $fieldsnum = 0; //返回字段数  
  12. private $rowsnum = 0; //返回结果数  
  13. private $rowsrst = ''; //返回单条记录的字段数组  
  14. private $filesarray = array(); //返回字段数组  
  15. private $rowsarray = array(); //返回结果数组  
  16. private $idusername=array();  
  17. private $idsubtitle=array();  
  18. //初始化类  
  19. function __construct($host='',$name='',$pwd='',$dbase=''){  
  20. if($host != '')  
  21. $this->host = $host;  
  22. if($name != '')  
  23. $this->name = $name;  
  24. if($pwd != '')  
  25. $this->pwd = $pwd;  
  26. if($dbase != '')  
  27. $this->dbase = $dbase;  
  28. $this->init_conn();  
  29. }  
  30. //链接数据库  
  31. function init_conn(){  
  32. $this->conn=@mysql_connect($this->host,$this->name,$this->pwd);  
  33. @mysql_select_db($this->dbase,$this->conn);  
  34. mysql_query("set names utf8");  
  35. }  
  36. //查询结果  
  37. function mysql_query_rst($sql){  
  38. if($this->conn == ''){  
  39. $this->init_conn();  
  40. }  
  41. $this->result = @mysql_query($sql,$this->conn);  
  42. } 
  43.  
  44.  
  45. //取得查询结果字段数目  
  46. function getfieldsnum($sql){  
  47. $this->mysql_query_rst($sql);  
  48. $this->fieldsnum = @mysql_num_fields($this->result);  
  49. }  
  50. //取得查询结果行数目  
  51. function getrowsnum($sql){  
  52. $this->mysql_query_rst($sql);  
  53. if(mysql_errno() == 0){  
  54. return @mysql_num_rows($this->result);  
  55. }else{  
  56. return '';  
  57. }  
  58. }  
  59. //取得记录数组有索引(单条记录)  
  60. function getrowsrst($sql){  
  61. $this->mysql_query_rst($sql);  
  62. if(mysql_error() == 0){  
  63. $this->rowsrst = mysql_fetch_array($this->result,mysql_assoc);  
  64. return $this->rowsrst;  
  65. }else{  
  66. return '';  
  67. }  
  68. }  
  69. //取得记录数组有索引(多条记录)全部  
  70. function getrowsarray($sql){  
  71. $this->mysql_query_rst($sql);  
  72. if(mysql_errno() == 0){  
  73. while($row = mysql_fetch_array($this->result,mysql_assoc)) {  
  74. $this->rowsarray[] = $row;  
  75. }  
  76. return $this->rowsarray;  
  77. }else{  
  78. return '';  
  79. }  
  80. }  
  81. //更新、删除、添加记录数,返回影响到的行数  
  82. function uidrst($sql){  
  83. if($this->conn == ''){  
  84. $this->init_conn();  
  85. }  
  86. @mysql_query($sql);  
  87. $this->rowsnum = @mysql_affected_rows();  
  88. if(mysql_errno() == 0){  
  89. return $this->rowsnum;  
  90. }else{  
  91. return '';  
  92. }  
  93. }  
  94. //获取对应的字段值,一条数字索引,mysql_array_rows才是带字段索引  
  95. function getfields($sql,$fields){  
  96. $this->mysql_query_rst($sql);  
  97. if(mysql_errno() == 0){  
  98. if(mysql_num_rows($this->result) > 0){  
  99. $tmpfld = @mysql_fetch_row($this->result);  
  100. $this->fields = $tmpfld[$fields]; 
  101.  
  102. }  
  103. return $this->fields;  
  104. }else{  
  105. return '';  
  106. }  
  107. } 
  108.  
  109. //错误信息  
  110. function msg_error(){  
  111. if(mysql_errno() != 0) {  
  112. $this->msg = mysql_error();  
  113. }  
  114. return $this->msg;  
  115. }  
  116. //释放结果集  
  117. function close_rst(){  
  118. mysql_free_result($this->result);  
  119. $this->msg = '';  
  120. $this->fieldsnum = 0;  
  121. $this->rowsnum = 0;  
  122. $this->filesarray = '';  
  123. $this->rowsarray = '';  
  124. $this->idsubtitle='';  
  125. $this->idusername='';  
  126. }  
  127. //关闭数据库  
  128. function close_conn(){  
  129. $this->close_rst();  
  130. mysql_close($this->conn);  
  131. $this->conn = '';  
  132. }  
  133. }  
  134. ?> 

PHP实例方法如下:

  1. <?php  
  2. $conne = new opmysql();  
  3. $conne-> getrowsarray($sql);  
  4. $conne-> close_conn();  
  5. $password=”123456一二三四五六”;  
  6. echo md5($password.”www.phpfensi.com”);//输出为32位的密文,是没有解密函数的,可以实现简单的加密功能。  
  7. ?> 

mysql数据库类型主要是:char(固定空间字符串,多大就是多少个中文字符)、varchar(可变空间字符串,多大就是初始化多少个中文字符)、int(整数多大就是多少位)、float(浮点数)、timestamp(日期,可选建立时自动创建,输出时就已经是格式化过的date)、text(文本)、bool(布尔型).

写sql语句时sum()可以统计值;order by 'id' desc limit 10,10等要活用,在phpmyadmin学一下sql语句增删改查就行了.

Tags: Mysql 数据库操作

分享到: