简单的MYsql操作类

  1. * 1、连接服务器 2、各类sql动作
  2. */
  3. class mysql{
  4. private $host; //服务器地址
  5. private $name; //用户名称
  6. private $pass; //密码
  7. private $table; //连接数据库教程
  8. private $jiema; //设置解码
  9. private $ztime; //设置服务器的时区
  10. //构造函数
  11. function __construct($host,$name,$pass,$table,$jiema,$ztime){
  12. $this -> host = $host ;
  13. $this -> name = $name ;
  14. $this -> pass = $pass ;
  15. $this -> table = $table ;
  16. $this -> jiema = $jiema ;
  17. $this -> ztime = $ztime ;
  18. $this -> connect();
  19. }
  20. //数据库连接和设置
  21. function connect(){
  22. $link=@mysql_connect($this->host,$this->name,$this->pass) or die ("连接服务器失败");
  23. @mysql_select_db($this->table,$link) or die("连接数据失败");
  24. @mysql_query("set names '$this->jiema'");
  25. @date_default_timezone_set("$this->ztime");
  26. }
  27. //执行操作
  28. function query($sql) {
  29. if(!($query = @mysql_query($sql))) $this->show($sql);
  30. return $query;
  31. }
  32. //显示信息
  33. function show($message = '', $sql = '') {
  34. if(!$sql) echo $message;
  35. else echo $message.'<br>'.$sql;
  36. }
  37. //取得数据集的某个值
  38. function result($query,$row,$values) {
  39. return @mysql_result($query,$row,$values);
  40. }
  41. //取得数据集的某个值
  42. function get_values($table,$row,$values) {
  43. $query = $this -> query("select * from $table");
  44. $returnvalues = mysql_result($query,$row,$values);
  45. return $returnvalues;
  46. }
  47. //取得数据集的行数
  48. function num_rows($query) {
  49. return @mysql_num_rows($query);
  50. }
  51. //循环读取数据
  52. function fetch($query) {
  53. return @mysql_fetch_array($query);
  54. }
  55. //最后一次插入纪录的id值
  56. function insert_id() {
  57. return mysql_insert_id();
  58. }
  59. //取得数据集中的一行
  60. function fetch_row($query) {
  61. return mysql_fetch_row($query);
  62. }
  63. //插入一条数据
  64. function fn_insert($table,$name,$value){
  65. if($this->query("insert into $table ($name) values ($value)")){
  66. return true;
  67. }else{
  68. return false;
  69. }
  70. }
  71. //插入任意数据
  72. function sql_insert($tbname,$postvalues){
  73. foreach ($postvalues as $key => $value) {
  74. $postvalue .= "`".$key."`".",";
  75. $sqlvalue .= "'".$value."',";
  76. }
  77. $sqlfield = mb_substr("$postvalue",0,-1,'gbk');
  78. $sqlvalue = mb_substr("$sqlvalue",0,-1,'gbk');
  79. if($this-> fn_insert("$tbname","$sqlfield","$sqlvalue")){
  80. return true;
  81. }else{
  82. return false;
  83. }
  84. }
  85. //修改万能数据
  86. function sql_update($table,$postvalues,$wwhere){
  87. foreach ($postvalues as $key=>$value) {
  88. $sqlfield .= $key."="."'".$value."'".",";
  89. }
  90. $sqlfield= mb_substr("$sqlfield",0,-1,'gbk');
  91. if($this->fn_update("$table","$sqlfield","$wwhere")){
  92. return true;
  93. }else{
  94. return false;
  95. }
  96. }
  97. //修改一条数据
  98. function fn_update($table,$value,$wwhere){
  99. if($this->query("update $table set $value where $wwhere")){
  100. return true;
  101. }else{
  102. return false;
  103. }
  104. }
  105. //删除一条数据
  106. function sql_delete($table,$wwhere){
  107. if($this->query("delete from $table where $wwhere")){
  108. return true;
  109. }else{
  110. return false;
  111. }
  112. }
  113. //关闭数据连接
  114. function close() {
  115. return mysql_close();
  116. }//开源代码phpfensi.com
  117. }
  118. $db = new mysql($location['host'],$location['hostname'],$location['hostpass'],$location['table'],$location['jiema']