Talk less and show code:
<?php
/**
* The following code is used for encapsulation of database operations classes
*
* @author Rex<[email protected]>
* @version 1.0
* @since 2015
*/
Class mysql{
Database connection return value
Private $conn;
/**
* [Constructor, return value to $conn]
* @param [string] $hostname [host name]
* @param [string] $username [user name]
* @param [string] $password [password]
* @param [string] $dbname [database name]
* @param [string] $charset [Character set]
* @return [NULL]
*/
function __construct ($hostname, $username, $password, $dbname, $charset = ' utf8 ') {
$conn = @mysql_connect ($hostname, $username, $password);
if (! $conn) {
Echo ' connection failed, please contact Administrator ';
Exit
}
$this->conn = $conn;
$res = mysql_select_db ($dbname);
if (! $res) {
Echo ' connection failed, please contact Administrator ';
Exit
}
Mysql_set_charset ($charset);
}
function __destruct () {
Mysql_close ();
}
/**
* [GetAll get all Information]
* @param [string] $sql [SQL statement]
* @return [Array] [returns a two-dimensional array]
*/
function GetAll ($sql) {
$result = mysql_query ($sql, $this->conn);
$data = Array ();
if ($result && mysql_num_rows ($result) >0) {
while ($row = Mysql_fetch_assoc ($result)) {
$data [] = $row;
}
}
return $data;
}
/**
* [GetOne get a single data]
* @param [string] $sql [SQL statement]
* @return [Array] [returns a one-dimensional array]
*/
function GetOne ($sql) {
$result = mysql_query ($sql, $this->conn);
$data = Array ();
if ($result && mysql_num_rows ($result) >0) {
$data = Mysql_fetch_assoc ($result);
}
return $data;
}
/**
* [GetOne get a single data]
* @param [string] $table [table name]
* @param [string] $data [by the field name when the key, property when the key value of the one-dimensional array]
* @return [Type] [returns FALSE or the ID of the inserted data]
*/
function Insert ($table, $data) {
$str = ";
$str. = "INSERT into ' $table '";
$str. = "('". Implode ("', '", Array_keys ($data)). " `) ";
$str. = "VALUES";
$str. = "('". Implode ("', '", $data). "')";
$res = mysql_query ($str, $this->conn);
if ($res && mysql_affected_rows () >0) {
return mysql_insert_id ();
}else{
return false;
}
}
/**
* [Update updates database]
* @param [string] $table [table name]
* @param [Array] $data [Updated data,a one-dimensional array of key values by field name when the property is a key]
* @param [string] $where [condition, ' field name ' = ' field property ']
* @return [Type] [update successfully returns the number of rows affected, update failed returns false]
*/
function Update ($table, $data, $where) {
$sql = ' UPDATE '. $table. ' SET ';
foreach ($data as $key = = $value) {
$sql. = "' {$key} ' = ' {$value} ',";
}
$sql = RTrim ($sql, ', ');
$sql. = "WHERE $where";
$res = mysql_query ($sql, $this->conn);
if ($res && mysql_affected_rows ()) {
return Mysql_affected_rows ();
}else{
return false;
}
}
/**
* [Delete delete data]
* @param [string] $table [table name]
* @param [string] $where [condition, ' field name ' = ' field property ']
* @return [Type] [successfully returns the number of rows affected, failure returns false]
*/
Function del ($table, $where) {
$sql = "DELETE from ' {$table} ' WHERE {$where}";
$res = mysql_query ($sql, $this->conn);
if ($res && mysql_affected_rows ()) {
return Mysql_affected_rows ();
}else{
return false;
}
}
}
Instantiate class:
<?php
Contains database operation class files
Include ' mysql.class.php ';
Set Incoming parameters
$hostname = ' localhost ';
$username = ' root ';
$password = ' 123456 ';
$dbname = ' AISI ';
$charset = ' UTF8 ';
Instantiating an Object
$db = new Mysql ($hostname, $username, $password, $dbname);
Get a piece of data
$sql = "SELECT count (as_article_id) as Count from As_article where as_article_type_id=1";
$count = $db->getone ($sql);
Get more than one piece of data
$sql = "SELECT * from As_article where as_article_type_id=1 the order by as_article_addtime desc limit $start, $limit";
$service = $db->getall ($sql);
Inserting data
$arr = Array (
' As_article_title ' = ' database Operation class ',
' As_article_author ' = ' Rex ',
);
$res = $db->insert (' as_article ', $arr);
Update data
$arr = Array (
' As_article_title ' = ' Instantiate object ',
' As_article_author ' = ' Lee ',
);
$where = "As_article_id=1";
$res = $db->update (' as_article ', $arr, $where);
Delete data
$where = "As_article_id=1";
$res = $db->del (' as_article ', $where);
?>
Finish the code and say a few words about it.
First of all, this white demo part of the code belongs to the simpler database encapsulation class, more suitable for beginners. Because the level is limited, forgive me.
Then say a little more about the whole idea. The encapsulation of the entire class, containing a private property $conn and several manipulation functions that connect to the database. $conn when an object is instantiated, a resource-type connection handle is returned by the constructor when it processes the passed-in parameter. The database can then be modified by the corresponding method of invoking the instantiated object. The GetOne method passes in the SQL statement of the $sql to query the single data and returns a one-dimensional array; The GetAll method also passes in the SQL statement to query multiple data and return a two-dimensional array; Insert method passes in the table name and associative array, returns the Boolen type or inserts the data corresponding index The Update method passes in the table name, associative array, and condition, returns the number of Boolen or rows affected, and the Del method passes in the table name and condition, returning the Boolen type.
That's all,but not the all. Interested friends can optimize the way that GetOne and GetAll pass directly into SQL statements as parameters.
MySQL database operation class in PHP