PHP class for operating MySQL database
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-25 09:06:28
Original
1049 people have browsed it
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
|
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31