The example in this article describes the MySQL class implemented in PHP based on a single example mode. Share to everyone for your reference, specific as follows:
<?php defined (' ACC ') | |
Exit (' Access Denied ');
Encapsulates the MySQL operation class, including the connection function, and the query function.
Class MySQL extends absdb{protected static $ins = NULL; protected $host; Host name protected $user; User name protected $passwd; Password protected $db; Database name protected $port;
Port protected $conn = NULL;
Inside operation, get an object public static function Getins () {if (self:: $ins = = null) {self:: $ins = new self ();
} $conf = Conf::getins ();
Self:: $ins->host = $conf->host;
Self:: $ins->user = $conf->user;
Self:: $ins->passwd = $conf->pwd;
Self:: $ins->db = $conf->db;
Self:: $ins->port = $conf->port;
Self:: $ins->connect ();
Self:: $ins->select_db ();
Self:: $ins->setchar ();
Return self:: $ins; ///Do not allow external to do new operation, protected function __construct () {}//Connection database public function connect () {$this->conn =
@mysql_connect ($this->host, $this->user, $this->passwd, $this->port);
if (! $this->conn) {$error = new Exception (' Database Not connected ', 9);
Throw $error;
}//Send SQL query Public function query ($sql) {$rs = mysql_query ($sql, $this->conn);
if (! $rs) {log::write ($sql);
return $rs;
//Encapsulates a GetAll method//parameter: $sql//return: Array,false Public Function GetAll ($sql) {$rs = $this->query ($sql);
if (! $rs) {return false;
} $list = Array ();
while ($row = Mysql_fetch_assoc ($rs)) {$list [] = $row;
return $list;
//Encapsulates a GetRow method//parameter: $sql//return: Array,false Public Function GetRow ($sql) {$rs = $this->query ($sql);
if (! $rs) {return false;
Return Mysql_fetch_assoc ($RS); //Encapsulates a GetOne method,//parameter: $sql//return: INT,STR (single value) public function GetOne ($sql) {$rs = $this->query ($sql)
;
if (! $rs) {return false;
} $tmp = Mysql_fetch_row ($RS);
return $tmp [0]; //Encapsulates a afftect_rows () method//parameter: no//returns int affected rows Public Function affected_rows () {
Return Mysql_affected_rows ($this->conn);
//Returns the value of the newly generated Auto_increment column public function last_id () {return mysql_insert_id ($this->conn);
///Library functions Public Function select_db () {$sql = ' use '. $this->db;
return $this->query ($sql);
SetChar () {$sql = ' set names UTF8 ' for the function of the character set
return $this->query ($sql); //Automatically generate INSERT statement, UPDATE statement and execute public function Autoexecute ($data, $table, $act = ' Insert ', $where = ') {if ($act = = ' Inse RT ') {$sql = ' insert INTO '. $table.
' (';
$sql. = Implode (', ', (Array_keys ($data));
$sql. = ') values (\ ';
$sql. = Implode ("', '", Array_values ($data));
$sql. = "')";
else if ($act = = ' Update ') {if (!trim ($where)) {return false; } $sql = ' Update '. $table.
' Set ';
foreach ($data as $k => $v) {$sql. = $k;
$sql. = ' = ';
$sql. = "'" $v. "',";
$sql = substr ($sql, 0,-1); $sql. = ' where ';
$sql. = $where;
else {return false;
}//return $sql;
return $this->query ($sql);
}
}
For more information about PHP database operations interested readers can view the site topics: "Php+mysql database operations to get Started", "PHP based on PDO Operation Database skills summary" and "PHP common database Operation Skills Summary"
I hope this article will help you with the PHP program design.