/**
* Database Tool Class
* 1. Connect to the database
* 2. Perform the function of adding and deleting changes and error
* 3. No parameter pass is to use the default information connection
*/
Class MySQL {
Private $link = null; Log Connection Resources
Private $host;
Private $port;
Private $user;
Private $pass;
Private $charset;
Private $dbname; Set 6 private properties to store connection properties
Singleton 1, set private static property, store the Singleton object
private static $instance = null;
Singleton 2, privatization of the construction method
Private function __construct ($conf) {
Preserve properties and set default values
$this->host= $conf [' Host ']? $conf [' Host ']: "localhost";
$this->port= $conf [' Port ']? $conf [' Port ']: ' 3306 ';
$this->user= $conf [' user ']? $conf [' User ']: "root";
$this->pass= $conf [' Pass ']? $conf [' Pass ']: "ZHANGHD";
$this->charset= $conf [' CharSet ']? $conf [' CharSet ']: "UTF8";
$this->dbname= $conf [' dbname ']? $conf [' dbname ']: "MVC";
$this->connect ();
}
Singleton 3, set a static method and determine if a new object is needed, and return
static function Getdb ($conf) {
if (Empty (self:: $instance)) {
Self:: $instance = new self ($conf);
}
Return self:: $instance;
}
Change Database
function Select_database ($db) {
$this->query ("Use $db");
$this->dbname= $db;
}
Change the character set of a connection
function Select_charset ($charset) {
$this->query ("Set names $charset");
$this->charset = $charset;
}
Close connection
function Close () {
Mysql_close ($this->link);
}
This method specializes in SQL statements, and execution succeeds and returns, failure ends
Private function Query ($sql) {
$result = mysql_query ($sql, $this->link);
if ($result = = = False) {
echo "<p> error occurred, please refer to:";
echo "<br> Error Statement:". $sql;
echo "<br> error Hint:". mysql_error ();
echo "<br> error code:". Mysql_errno ();
Die (); Failure terminates the program
}else {
return $result;
}
}
This method is used to execute a non-return and delete-search statement
function exec ($sql) {
$result = $this->query ($sql);
return true;
}
The method can execute a SELECT statement that returns multiple rows of data and return the data as a "two-dimensional array"
function GetRows ($sql) {
$result = $this->query ($sql);
Returns a two-dimensional array. At this point, $result is the result set.
while ($res = Mysql_fetch_assoc ($result)) {
$arr []= $res;
}
return $arr;
}
Returns one row of multiple columns of data
function Getonerow ($sql) {
$result = $this->query ($sql);
Returns a set of data. At this point, $result is the result set.
if ($rec = Mysql_fetch_assoc ($result)) {
return $rec;
}
return Array ();
}
Returns one row of multiple columns of data
function Getonedate ($sql) {
$result = $this->query ($sql);
Returns a set of data. At this point, $result is the result set.
if ($rec = Mysql_fetch_row ($result)) {
return $rec [0];//If there is no data, the row is returned
}
Return false;//indicates no data
}
To save a connection resource to a local hard disk
function __sleep () {
Return Array (' Host ', ' Port ', ' user ', ' Pass ', ' CharSet ', ' dbname ');
}
To tune the local storage connection resource to connect to the database system again
function __wakeup () {
$this->connect ();
}
Connect to the database and store the connection resources.
Private Function Connect () {
$this->link = @mysql_connect ("{$this->host}:{$this->port}", "{$this->user}", "{$this->pass}") or Die ( ' Database server Connection failed!!! ‘);
$this->select_charset ($this->charset);
$this->select_database ($this->dbname);
}
}
A database tool class that you write yourself