PHP數據庫連接及操作類

<?php
/*
* Created on 2012-2-12
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
class mysql{
  private $host;//服務器名稱
  private $name;//登錄名
  private $password;//登錄密碼
  private $database;//要連接的數據庫
  private $ut;//編碼方式
function __construct($host,$name,$password,$database,$ut){
   $this->host=$host;
   $this->name=$name;
   $this->password=$password;
   $this->database=$database;
  $this->ut=$ut;
   $this->connect();//初始化的時候直接調用連接函數
  }
  function connect(){
   $link=mysql_connect($this->host,$this->name,$this->password) or die(mysql_error());//連接MYSQL
   mysql_select_db($this->database,$link)or die(mysql_error());//連接數據庫
   mysql_query("SET NAMES '$this->ut'");//設置編碼方式
  echo "database connect sucessful!";
   }
  function query($v){//如果要是換成其他數據庫的話,直接改變裏面的方法即可,體現封裝的好處
   return mysql_query($v);
  }
  function error(){//把錯誤提示也封裝起來,方便調用
   return mysql_error();
  }
  function insert($table,$name,$value){//插入函數
   mysql_query("insert into $table($name) values($value)" );
  }
  }
//////////////////////////////////////其餘函數根據項目需要自行編寫
?>

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