Home  >  Article  >  Backend Development  >  php mysql database connection class_PHP tutorial

php mysql database connection class_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:59:27814browse

Provides a simple implementation of the connection class that uses the PHP constructor to automatically create connections and delete operations. Friends in need can refer to it.

The code is as follows Copy code

class mysql {
Private $db_host; //Database host
Private $db_user; //Database user name
Private $db_pwd; //Database username and password
Private $db_database; //Database name
Private $conn; //Database connection identifier;
Private $result; //Result resource identifier of executing query command
Private $sql; //sql execution statement
Private $row; //Number of entries returned
Private $coding; //Database encoding, GBK, UTF8, gb2312
Private $bulletin = true; //Whether to enable error logging
Private $show_error = true; //During the testing phase, all errors are displayed, which has security risks and is closed by default
Private $is_error = false; //Whether to terminate immediately when an error is detected, the default is true, it is recommended not to enable it, because it is very distressing for users to not see anything when there is a problem

/*Constructor*/
Public function __construct($db_host, $db_user, $db_pwd, $db_database, $conn, $coding) {
           $this->db_host = $db_host;
           $this->db_user = $db_user;
           $this->db_pwd = $db_pwd;
            $this->db_database = $db_database;
$this->conn = $conn;
           $this->coding = $coding;
            $this->connect();
}

/*Database connection*/
Public function connect() {
If ($this->conn == "pconn") {
//Permanent link
$this->conn = mysql_pconnect($this->db_host, $this->db_user, $this->db_pwd);
         } else {
                                                                                                                                                                                                                                                to $this->conn = mysql_connect($this->db_host, $this->db_user, $this->db_pwd);
}

if (!mysql_select_db($this->db_database, $this->conn)) {

If ($this->show_error) {
$ This-& gt; show_error ("Database is unavailable:", $ this-& gt; db_database);
            }
}
mysql_query("SET NAMES $this->coding");
}

/*Database execution statement, any sql statement such as query addition, modification, deletion, etc. can be executed*/

Public function query($sql) {
           if ($sql == "") {
​​​​​​​ $this->show_error("SQL statement error:", "SQL query statement is empty");
}
           $this->sql = $sql;

$result = mysql_query($this->sql, $this->conn);

if (!$result) {
                         // Used in debugging, it will automatically print out when an error occurs in the sql statement
If ($this->show_error) {
                       $this->show_error("Error SQL statement: ", $this->sql);
            }
         } else {
                 $this->result = $result;
}
          return $this->result;
}

/*Create and add a new database*/
Public function create_database($database_name) {
          $database = $database_name;
           $sqlDatabase = 'create database ' . $database;
           $this->query($sqlDatabase);
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631326.htmlTechArticleProvides a simple connection class that uses the PHP constructor to automatically create connection and delete operations. Friends in need You can refer to it. The code is as follows Copy code class mysql { private $d...
Statement:
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