class mysql { private $host; private $user; private $pass; private $database; private $charset; function __construct($host,$user,$pass,$database,$charset) { $this--->host=$host; $this->user=$user; $this->pass=$pass; $this->database=$database; $this->charset=$charset; $this->connect(); } private function connect()//串連函數 { mysql_connect($this->host,$this->user,$this->pass) or die ("串連資料庫伺服器失敗!"); mysql_select_db($this->database) or die ("串連資料庫失敗!"); mysql_query("set names $this->charset"); } function select($sql,$tab,$col,$value)//選擇函數 { $select=mysql_query("select $sql from $tab where $col=$value"); $row=mysql_fetch_array($select); return $row; } function insert($tab,$col,$value)//插入資料函數 { mysql_query("INSERT INTO $tab($col)values($value)"); } function update($tab,$col,$new_value,$colm,$value)//更新資料函數 { mysql_query("UPDATE $tab SET $col=$new_value where $colm=$value"); } function delete($tab,$col,$value)//刪除資料函數 { mysql_query("DELETE FROM $tab where $col=$value"); } function close()//關閉串連函數 { mysql_close(); } } //$mysql=new mysql("127.0.0.1","root","120360150","test","gbk");串連資料庫 //$sql=$mysql->select("*","admin","id","3");//選擇資料 //print_r($sql);//列印返回的數組 //$mysql->insert("admin","user_name,user_pass","'123','123'");插入資料 //$mysql->update("admin","user_pass","22","id","3");更新資料 //$mysql->delete("admin","id","4");刪除資料 //$mysql->close();關閉串連 ?> |