mysql_class.php

<?php
/*
*class mysql
*
*/

class mysql{
    private $host;
 private $name;
 private $password;
 private $table;
 private $ut;

//初始化對象
 function __construct($host,$name,$password,$table,$ut){
  $this->host=$host;
     $this->name=$name;
  $this->password=$password;
  $this->table=$table;
  $this->ut=$ut;
  $this->connect();
 }

//數據庫連接方法
 function connect(){
        $link=mysql_connect($this->host,$this->name,$this->password) or die ($this->error());
  mysql_select_db($this->table,$link) or die ("沒有該數據庫:".$this->table);
  mysql_query("SET NAMES '$this->ut'");
  
 }

//封裝起來的基本操作
 function query($v){
     return mysql_query($v);
 }

 function error(){
     return mysql_error();
 }

 function affected_rows(){
     return mysql_affect_rows();
 }

 function result($query,$row){
     return mysql_result($query,$row);
 }

 function num_rows($query){
     return $mysql_num_rows($query);
 }

 function num_fields($query){
     return mysql_num_fields($query);
 }

 function free_result($query){
     return mysql_free_result($query);
 }

 function insert_id(){
     return mysql_insert_id();
 }
 
 function fetch_row($query){
     return mysql_fetch_row($query);
 }

 function version(){
     return mysql_get_server_info();
 }

 function close(){
     return mysql_close();
 }


//封裝起來的功能操作
   
    //增
 function fn_insert($table,$name,$value){
  //insert into[表名] values('','',......順序排列的數據);
     $this->query("insert into $table ($name) values ($value)");
 }
   
 //刪
    function fn_del($table,$data){
  //delete from [表名] where ([條件]);
     $this->query("delete from $table where (id=$data)");
 }
   
 //查
 function fn_search($table){
     $this->query("select * from $table");
 }
   
 //改 UPDATE [表名] SET [修改內容如name = 'Mary'] WHERE [條件];
 function fn_update($table,$name1,$value,$name2,$id){
     $this->query("update $table set $name1='$value' where $name2='$id'");
 }
}

//連接數據庫 
$db=new mysql('localhost','root','123456','db70100160',"GBK");

//增加記錄  
//$db->fn_insert('test','id,title,dates',"'6','我插入的信息',now()");

//刪除記錄
//$db->fn_del('test','1');

//查詢記錄
//$db->fn_search('test');

//修改記錄
//$db->fn_update('test','title','laijian2','id','4');
 
 


?>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章