PHP数据库连接及操作类

来源:互联网 发布:搜狐网络大厦11层 编辑:程序博客网 时间:2024/05/16 10:39

<?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)" );
  }
  }
//////////////////////////////////////其余函数根据项目需要自行编写
?>

原创粉丝点击