<?php
/* $obj =new obj (param ...)
$obj->set_charset ()
$obj->connect_errno
$obj->connect_error
$obj->query ()
$obj->errno
$obj->error
$result = $obj->query (); Mysqli_result Object
$result->fetch_all ()
$result->fetch_array ()
$obj->close () */
Class Dbmysqlimanage extends mysqli{
Public $link =null;
Public $message;
/**
* Operation Tip Information
* @param unknown $mes information
* @param string $flag success and failure flag
* @return Boolean
*/
Public Function GetMessage ($mes, $flag =false) {
if ($flag) {
$this->message.= ' <div style= "color:green;font-size:14px" > ". $mes. ' </div> ';
return true;
}else{
$this->message.= ' <div style= "color:red;font-size:14px" > ". $mes. ' </div> ';
return false;
}
}
/**
* Initialize link
* @param unknown $host host address
* @param unknown $username users
* @param unknown $password password
* @param unknown $dbName database name
* @param string $charset Character Set
*/
Public function __construct ($host, $username, $password, $dbName, $charset = ' utf8 ') {
$this->link=new mysqli ($host, $username, $password, $dbName);
if ($this->link->connect_error) {
return $this->getmessage (' Database connection failure error number '. $this->link->connect_errno.
' Error message: '. Iconv (' GBK ', ' utf-8 ', $this->link->connect_error));
}else{
$this->getmessage (' Database connection succeeded ', true);
}
$char = $this->link->set_charset ($charset);
if ($char ===false) return $this->getmessage (' Wrong encoding set error number: '.
$this->link->mysqli_errno. ' Error message: ' $this->link->mysqli_error);
$this->getmessage (' encoding set succeeded ', true);
}
/**
* Perform additions and deletions and change statements
* @param unknown $sql SQL statements
* @return Boolean
*/
Public Function Execsql ($sql) {
$sql =trim ($sql);
$match =preg_match ('/^ (insert|update|delete)/', $sql);
if ($match ===0) {
return $this->getmessage (__function__. ' () method only support additions and deletions to the operation ');
}else{
$result = $this->link->query ($sql);
if ($result ===false) {
return $this->getmessage ($sql. ' Statement Error Error number: '.
$this->link->errno. ' Error message: ' $this->link->error);
}else{
return $this->getmessage (' Operation succeeded ', ' true ');
}
}
}
/**
* Query a record
* @param unknown $sql SQL statements
* @return Failure: boolean| success: one-dimensional array
*/
Public Function Getonedata ($sql) {
$sql =trim ($sql);
$match =preg_match ('/^select/', $sql);
if ($match ===0) {
return $this->getmessage (__function__. ' () method only supports query operation ');
}else{
$result = $this->link->query ($sql); Mysqli_result Object
if (!is_object ($result)) {
return $this->getmessage ($sql. ' Statement Error Error number: '.
$this->link->errno. ' Error message: ' $this->link->error);
}else{
$row = $result->fetch_array (MYSQLI_ASSOC);
if ($result->fetch_array (MYSQLI_ASSOC)) return $this->getmessage (__function__. ' () can only query one data error number: '.
$this->link->errno. ' Error message: ' $this->link->error);
$this->getmessage (' Query a record succeeds ', true);
Unset ($result);
return $row;
}
}
}
/**
* Query multiple records
* @param unknown $sql SQL statements
* @return Failure: boolean| success: two-bit array
*/
Public Function Getmoredata ($sql) {
$sql =trim ($sql);
$match =preg_match ('/^select/', $sql);
if ($match ===0) {
return $this->getmessage (__function__. ' () method only supports query operation ');
}else{
$result = $this->link->query ($sql); Mysqli_result Object
if (!is_object ($result)) {
return $this->getmessage ($sql. ' Statement Error Error number: '.
Mysqli_errno ($this->link). ' Error message: '. Mysqli_error ($this->link));
}else{
$moreRow = $result->fetch_all (MYSQLI_ASSOC);
$this->getmessage (' Get Multiple records succeeded ', true);
Unset ($result);
return $moreRow;
}
}
}
/**
* Close Connection Resources
* @return Boolean
*/
Public Function Closedb () {
$close = $this->link->close ();
if ($close ===false) return $this->getmessage (' Database connection shutdown failed ');
return $this->getmessage (' database connection closed successfully ', true);
}
/**
* destructor
*
*/
Public Function __destruct () {
unset ($this->message);
}
}
Mysqli Object Style Encapsulation