操作MySQL数据库的php类
时间:2021-07-01 10:21:17
帮助过:12人阅读
操作mysql数据库的php类
- ///////////////////数据库连接类////////////////////
- class connect{
- private $host;//主机名
- private $name;//用户名
- private $pass;//密码
- private $conn;//连接句柄名
- private $db;//数据库句柄名
- private $dbname;//数据库名
- //===================================================================
- function open($addr,$dbuser,$psw){//连接主机
- $this->host=$addr;
- $this->name=$dbuser;
- $this->pass=$psw;
- $this->conn=mysql_connect($this->host,$this->name,$this->pass);
- }
-
- function opendb($database,$charset){//连接数据库
- $this->dbname=$database;
- mysql_query("set names ".$charset);//设置字符集
- $this->db=mysql_select_db($this->dbname,$this->conn);
- }
- function close(){//关闭主机连接
- mysql_close($this->conn);
- }
-
- //===================================================================
-
- function __construct($addr,$dbuser,$psw){
- $this->open($addr, $dbuser, $psw);
- }
-
- function __toString(){
- if($this->conn){
- $msg= "用户".$this->name."登录主机成功。";
- }else {
- $msg= "用户".$this->name."登录主机失败。";
- }
- if($this->db){
- $msg.= "连接".$this->dbname."数据库成功。";
- }else{
- $msg.= "连结".$this->dbname."数据库失败。";
- }
-
- return $msg;
- }
- function __call($n,$v){//错误方法吸收
- return "不存在".$n."()方法";
- }
-
- }
- ////////////////示例/////////////////////
- // $db=new connect("localhost", "root", "lijun");
- // $db->opendb("message", "utf8");
- // echo $db;
- // $db->close();
- // $db->open("localhost","root","lijun");
- // $db->opendb("message", "utf8");
- // echo $db->ji("er");
-
- ?>
|