PHP class that operates mysql database
- ///////////////////Database connection class//////////////////// /
- class connect{
- private $host;//Host name
- private $name;//Username
- private $pass;//Password
- private $conn;//Connection handle name
- private $db;//Database handle Name
- private $dbname;//Database name
- //====================================== ===============================
- function open($addr,$dbuser,$psw){//Connect to the host
- $this->host=$addr;
- $this->name=$dbuser;
- $this->pass=$psw;
- $this->conn=mysql_connect($this->host,$ this->name,$this->pass);
- }
-
- function opendb($database,$charset){//Connect to the database
- $this->dbname=$database;
- mysql_query("set names " .$charset);//Set the character set
- $this->db=mysql_select_db($this->dbname,$this->conn);
- }
- function close(){//Close the host connection
- mysql_close ($this->conn);
- }
-
- //==================================== ================================
-
- function __construct($addr,$dbuser,$psw){
- $this ->open($addr, $dbuser, $psw);
- }
-
- function __toString(){
- if($this->conn){
- $msg= "User".$this->name. "Login to the host successfully.";
- }else {
- $msg= "User ".$this->name." failed to log in to the host. ";
- }
- if($this->db){
- $msg.= "Connection to ".$this->dbname." database was successful. ";
- }else{
- $msg.= "Link ".$this->dbname." Database failed. ";
- }
-
- return $msg;
- }
- function __call($n,$v){//Error method absorption
- return "Does not exist".$n."() method";
- }
-
- }
-
- /////////////////Example/////////////////////
-
- // $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");
-
- ?>
Copy code
|