<?php
header (' Content-type:text/html;charset=utf-8 ');
Encapsulating MySQL Connection databasePhp_mysql
Encapsulating MySQL Connection database php_mysqli
Encapsulating MySQL Connection database php_pdo
Class db{
//Three private altogether
//private static property
private static $dbcon = false;//Store database Connection object
//Private method of construction
Private Function __construct () {
//Create links
$dbcon =mysql_connect (' 127.0.0.1 ', ' root ', ' root ');
//Select Database
mysql_select_db (' Yii_back ', $dbcon) or Die (' MySQL connect error ');
//Set character sets
mysql_query (' Set names UTF8 ');
}
//Private clone
Private Function __clone () {}
//Common static methods
Public static function getinstance () {
if (self:: $dbcon = = False) {
Self :: $dbcon = new self;
}
return self:: $dbcon;
}
/**
*Execute SQL statement
* @param $sql
* @return Source
**/
Public Function Query ($sql) {
Through mysql_query to achieve
$query = mysql_query ($sql);
Return to execution results
return $query;
}
/**
*Querying a fieldEg:select Name Select COUNT (*)
* @param $sql
* @return string or int
**/
Public Function GetOne ($sql) {
$query = $this->query ($sql);
Errors may occur
if (! $query) {
Error in SQL statement
echo ' SQL statement Error! <br/> ';
Echo ' ERROR number: '. Mysql_errno (). ' <br/> ';
Echo ' Error reason: '. Mysql_error (). ' <br/> ';
Exit
}
Return mysql_result ($query, 0);
}
/**
*querying a row of records
* @param $sql
* @return Array one-dimensional arrays
*MYSQL_FETCH_ASSOC Mysql_fetch_array Mysql_fetch_row
**/
Public Function GetRow ($sql, $type = "Assoc") {
$query = $this->query ($sql);
if (!in_array ($type, Array (' Assoc ', ' array ', ' Row '))) {
Die (' mysql_query error! ');
}
$funcname = "Mysql_fetch_". $type;
Return $funcname ($query);
}
/**
*querying more than one record
* @param $sql
* @return Array two-dimensional arrays
*MYSQL_FETCH_ASSOC Mysql_fetch_array Mysql_fetch_row
**/
Public Function GetAll ($sql) {
Invoking the SQL execution function
$query = $this->query ($sql);
$list =array ();
Traverse results
while ($arr = $this->getrowfromsource ($query)) {
while ($arr = Mysql_fetch_assoc ($query)) {
$list []= $arr;
}
return $list;
}
/**
*get the ID of the last insertion
**/
Public Function Getinsertid () {
return mysql_insert_id ();
}
/**
*New Data Method
* @param1 string $sql, the SQL statement to execute
* @param1 Array $data, the data to be added
* @return Self-Growth ID
**/
Public Function Insert ($table, $data =array ()) {
Print_r ($data);d ie;
$sql = "INSERT into". $table. " (". Implode (', ', Array_keys ($data)).") VALUES (' ". Implode (" ', ' ", Array_values ($data))."
Invoking the SQL execution function
$query = $this->query ($sql);
if ($query) {
Return data
return mysql_insert_id (); Get the self-growth ID of the last operation
}else{
return $sql;
}
}
/**
*Modifying Data methods
* @param string $table The data table name of the operation
* Data @param Array $data operation
* @param array $condition conditions
**/
Public Function Update ($table, $data, $condition =array ()) {
$where = ";
if (!empty ($condition)) {
foreach ($condition as $k = = $v) {
$where. = $k. " = ' ". $v." ' and ";
}
$where = ' where '. $where. ' 1=1 ';
}
$updatastr = ";
if (!empty ($data)) {
foreach ($data as $k = = $v) {
$updatastr. = $k. " = ' ". $v." ', ";
}
$updatastr = ' Set '. RTrim ($updatastr, ', ');
}
$sql = "Update {$table} {$updatastr} {$where}";
$query = $this->query ($sql);
if ($query) {
Echo ' modification succeeded ';
}else{
return $sql;
}
}
/**
*Delete Data method
* @param string $table The data table name of the operation
* @param array $condition conditions to delete
*/
Public Function Delete ($table, $condition) {
$where = ";
if (!empty ($condition)) {
foreach ($condition as $k = = $v) {
$where. = $k. " = ' ". $v." ' and ";
}
$where = ' where '. $where. ' 1=1 ';
}
$sql = "Delete from {$table} {$where}";
$query = $this->query ($sql);
if ($query) {
Echo ' Delete succeeded ';
}else{
return $sql;
}
}
/**
*Querying data methods
* @param string $table The data table name of the operation
* @param the criteria for the array $condition query
* @param array $field the field to query
*/
Public Function Select ($table, $condition =array (), $field = Array ()) {
$where = ";
if (!empty ($condition)) {
foreach ($condition as $k = = $v) {
$where. = $k. " = ' ". $v." ' and ";
}
$where = ' where '. $where. ' 1=1 ';
}
$fieldstr = ";
if (!empty ($field)) {
foreach ($field as $k = = $v) {
$fieldstr. = $v. ', ';
}
$fieldstr = RTrim ($fieldstr, ', ');
}else{
$FIELDSTR = ' * ';
}
$sql = "Select {$fieldstr} from {$table} {$where}";
$query = $this->query ($sql);
$resultRow = Array ();
$i = 0;
while ($row =mysql_fetch_assoc ($query)) {
foreach ($row as $k = = $v) {
$resultRow [$i] [$k] = $v;
}
$i + +;
}
return $resultRow;
}
}
PHP encapsulated MySQL database operation class