Useful MYSQL operations in PHP (Design Mode 1 ). It is very easy to operate a database using PHP, and PHPER can be used after getting started. However, when dealing with a large number of table operations, we get bored with many MYSQL statements, therefore, it is very easy to use PHP to operate a database. PHPER can be used after getting started. However, when dealing with a large number of table operations, we get bored with many MYSQL statements, therefore, we are eager to encapsulate a large number of database operations. So there is a database object ING.
First, create an interface.
Singleton. class. php
[Php]
/**
* @ Author tomyjohn
* @ Link
* @ License
* @ Version 1.0
* @ Copyright Copyright 2010 tomyjohn-tomyjohn.gicp.net
* @ Package singleton
*/
/**
* Database objects
*/
Interface singleton {
/**
* Generate database objects
* @ Returns object data object;
* @ Access public
*/
Public static function getInstance ();
}
Create an abstract class that abstracts all databases in a concise manner using five methods.
Db. class. php
[Php]
/**
* @ Author tomyjohn
* @ Link
* @ License
* @ Version 1.0
* @ Copyright Copyright 2010 tomyjohn-tomyjohn.gicp.net
* @ Package db
*/
/**
* Abstract DB Class
*/
Abstract class db {
/**
* Factory mode
* @ Param string $ type SQL type
* @ Returns object
* @ Access public
*/
Public static function factory ($ type ){
Return call_user_func (array ($ type, 'getinstance '));
}
/**
* Execute SQL statements
* @ Param string $ query SQL statement
* @ Return object resource or false;
* @ Access public
*/
Abstract public function execute ($ query );
/**
* Obtain the array returned by the SQL statement.
* @ Param string $ query SQL statement
* @ Return object resource or false;
* @ Access public
*/
Abstract public function get_array ($ query );
/**
* Obtain the execution ID of the previous statement.
* @ Param string $ query SQL statement
* @ Return integer number or false;
* @ Access public
*/
Abstract public function insert_get_id ($ query );
/**
* Convert Special characters
* @ Param string $ string
* @ Return string the processed string
* @ Access public
*/
Abstract public function clean ($ string );
}
I believe that here, we will all think about how to use the call_user_func method. Don't worry. let's look down.
Mysql. class. php
[Html]
/**
* @ Author tomyjohn
* @ Link
* @ License
* @ Version 1.0
* @ Copyright Copyright 2010 tomyjohn-tomyjohn.gicp.net
* @ Package db
*/
/**
* MYSQL database objects
*/
Class mysql extends db implements singleton {
/**
* @ Var $ instance object
* @ Access current class
*/
Protected static $ instance = null;
/**
* @ Var $ link resource
* @ Access current class
*/
Protected $ link;
/**
* Database instance
* @ Return $ self: instance object
*/
Public static function getInstance (){
If (is_null (self: $ instance )){
Self: $ instance = new self ();
}
Return self: $ instance;
}
/**
* Constructor
*/
Protected function _ construct (){
Global $ current_conf;
$ User = $ current_conf ['dbuser'];
$ Pass = $ current_conf ['dbpwd'];
$ Host = $ current_conf ['dbhost'];
$ Db = $ current_conf ['dbname'];
$ This-> link = mysql_connect ($ host, $ user, $ pass );
Mysql_set_charset ($ current_conf ['dbcharset'], $ this-> link );
Mysql_select_db ($ db );
}
/**
* Convert Special characters
* @ Param string $ string
* @ Return string the processed string
* @ Access public
*/
Public function clean ($ string ){
Return mysql_real_escape_string ($ string, $ this-> link );
}
/**
* Execute SQL statements
* @ Param string $ query SQL statement
* @ Return object resource or false;
* @ Access public
*/
Public function execute ($ query ){
Return mysql_query ($ query, $ this-> link );
}
/**
* Obtain the execution ID of the previous statement.
* @ Param string $ query SQL statement
* @ Return integer number or false;
* @ Access public
*/
Public function insert_get_id ($ query ){
$ This-> execute ($ query );
Return mysql_insert_id ($ this-> link );
}
/**
* Obtain the array returned by the SQL statement.
* @ Param string $ query SQL statement
* @ Return object resource or false;
* @ Access public
*/
Public function get_array ($ query ){
$ Result = $ this-> execute ($ query );
$ Return = array ();
If ($ result ){
While ($ row = mysql_fetch_array ($ result, MYSQL_ASSOC )){
$ Return [] = $ row;
}
}
Return $ return;
}
}
The current_conf array is in my project. In fact, it can also be replaced by your database username and password. after reading this, I think you should be clear about it, the class that inherits the database and implements the singleton interface can also be used in MSSQL, CMDL, and other databases. However, with this class, we can only change the operation database to this class.
[Html] view plaincopy
$ Connection = db: factory ('mysql ');
$ SQL = "SELECT * FROM table ";
$ Value_array = $ connection-> get_array ($ SQL );
[Html] view plaincopy
Print_r ($ value_array );
This solution solves the expansion and some repeated operations, but it is not very convenient. we should proceed further so that the database table is represented by an object, that is, database ING.
Dao. class. php
[Html]
Class dao {
/**
* @ Var $ values array stores database objects
* @ Access current class
*/
Protected $ values = array ();
/**
* @ Var $ suffix array stores database objects
* @ Access public
*/
Public $ suffix = '';
/**
* Constructor
*/
Public function _ construct ($ qualifier = null ){
Global $ current_conf;
$ This-> suffix = $ current_conf ['dbsuffix '];
If (! Is_null ($ qualifier )){
$ Conditional = array ();
If (is_numeric ($ qualifier )){
$ Conditional = array ('id' => $ qualifier );
}
Else if (is_array ($ qualifier )){
$ Conditional = $ qualifier;
}
Else {
Throw new Exception ('invalid type of qualifier given! ');
}
$ This-> populate ($ conditional );
}
}
Public function _ set ($ name, $ value ){
$ This-> values [$ name] = $ value;
}
Public function _ get ($ name ){
If (isset ($ this-> values [$ name]) {
Return $ this-> values [$ name];
}
Else {
Return null;
}
}
/**
* Parse instance parameters
* @ Param $ conditional obj
*/
Protected function populate ($ conditional ){
$ Connection = db: factory ('mysql ');
$ SQL = "SELECT * FROM {$ this-> suffix} {$ this-> table} WHERE ";
$ Qualifier = '';
Foreach ($ conditional as $ column => $ value ){
If (! Empty ($ qualifier )){
$ Qualifier. = 'and ';
}
$ Qualifier. = "'{$ column}' = '". $ connection-> clean ($ value )."'";
}
$ SQL. = $ qualifier;
$ Value_array = $ connection-> get_array ($ SQL );
If (! Isset ($ value_array [0]) {
$ Value_array [0] = array ();
}
Foreach ($ value_array [0] as $ key => $ value ){
$ This-> values [$ key] = $ value;
}
}
/**
* Save data
*/
Public function save (){
If (! $ This-> id ){
$ This-> create ();
}
Else {
Return $ this-> update ();
}
}
/**
* Add data www.2cto.com
*/
Public function create (){
$ Connection = db: factory ('mysql ');
$ SQL = "INSERT INTO {$ this-> suffix} {$ this-> table }("';
$ SQL. = implode ('','', array_keys ($ this-> values ));
$ SQL. = "') VALUES ('";
$ Clean = array ();
Foreach ($ this-> values as $ value ){
$ Clean [] = $ connection-> clean ($ value );
}
$ SQL. = implode ("','", $ clean );
$ SQL. = "')";
$ This-> id = $ connection-> insert_get_id ($ SQL );
}
/**
* Update data
* @ Return returns the result of the operation.
*/
Public function update (){
$ Connection = db: factory ('mysql ');
$ SQL = "UPDATE {$ this-> suffix} {$ this-> table} set ";
$ Updates = array ();
Foreach ($ this-> values as $ key => $ value ){
If ($ key! = 'Id '){
$ Updates [] = "'{$ key}' = '". $ connection-> clean ($ value )."'";
}
}
$ SQL. = implode (',', $ updates );
$ SQL. = "WHERE id = {$ this-> id }";
Return $ connection-> execute ($ SQL );
}
/**
* Delete data
* @ Return returns the result of the operation.
*/
Public function delete (){
$ Connection = db: factory ('mysql ');
$ SQL = "DELETE FROM {$ this-> suffix} {$ this-> table} WHERE ";
$ Qualifier = 'Id = '. $ this-> id;
$ SQL. = $ qualifier;
Return $ connection-> execute ($ SQL );
}
/**
* Convert an object to an array.
* @ Return array
*/
Public function object_to_array (){
Return $ this-> values;
}
}
If you see this, I believe everyone will want to inherit this class. Yes. if this class is inherited, each record can be converted into an object and can be processed in an object-oriented manner.
I write
News. dao. class
Class news extends dao {
Protected $ table = _ CLASS __;
}
Author: tomyjohn
...