Copy codeThe Code is as follows:
<? Php
Class Mysql
{
Private $ conn;
Private $ host;
Private $ username;
Private $ password;
Private $ dbname;
Private $ pconnect;
Private $ charset;
Public function _ construct (array $ params = null)
{
If (! Empty ($ params )){
Foreach ($ params as $ k => $ v ){
$ This-> $ k = $ v;
}
}
}
Public function connect ()
{
$ Fun = $ this-> pconnect? 'Mysql _ pconnect ': 'mysql _ connect ';
$ This-> conn = $ fun ($ this-> host, $ this-> username, $ this-> password );
$ This-> conn & $ this-> query ('set names'. $ this-> charset );
$ This-> conn & mysql_select_db ($ this-> dbname, $ this-> conn );
}
Public function getInstance ()
{
Return $ this-> conn;
}
Public function query ($ SQL)
{
Return mysql_query ($ SQL, $ this-> conn );
}
Public function fetchOne ($ SQL)
{
$ Data = $ this-> fetchRow ($ SQL );
Return $ data [0];
}
Public function fetchCol ($ SQL)
{
$ Tmp = $ this-> fetchAll ($ SQL, MYSQL_NUM );
Foreach ($ tmp as $ v ){
$ Data [] = $ v [0];
}
}
Public function fetchRow ($ SQL)
{
$ Result = $ this-> query ($ SQL );
$ Data = mysql_fetch_row ($ result );
Mysql_free_result ($ result );
Return $ data;
}
Public function fetchAssoc ($ SQL)
{
$ Result = $ this-> query ($ SQL );
$ Data = mysql_fetch_assoc ($ result );
Mysql_free_result ($ result );
Return $ data;
}
Public function fetchAll ($ SQL, $ type = MYSQL_ASSOC)
{
$ Result = $ this-> query ($ SQL );
While ($ tmp = mysql_fetch_array ($ result, $ type )){
$ Data [] = $ tmp;
}
Return $ data;
}
Public function fetchPairs ($ SQL)
{
$ Result = $ this-> query ($ SQL );
While ($ tmp = mysql_fetch_row ($ result )){
$ Data [$ tmp [0] = $ tmp [1];
}
Return $ data;
}
Public function insert ($ table, array $ bind)
{
$ Cols = array ();
$ Vals = array ();
Foreach ($ bind as $ col => $ val ){
$ Cols [] = $ col;
$ Vals [] = $ val;
Unset ($ bind [$ col]);
}
$ SQL = "INSERT"
. $ Table
. '(''. Implode ('', '', $ cols ).'')'
. 'Values (\ ''. implode ('\', \'', $ vals ).'\')';
$ Stmt = $ this-> query ($ SQL, $ this-> conn );
$ Result = $ this-> affectedRows ();
Return $ result;
}
Public function getLastInsertId ()
{
Return mysql_insert_id ($ this-> conn );
}
Public function affectedRows ()
{
Return mysql_affected_rows ($ this-> conn );
}
Public function update ($ table, array $ bind, $ where = '')
{
$ Set = array ();
Foreach ($ bind as $ col => $ val ){
$ Set [] = '''. $ col. "'='". $ val ."'";
}
$ SQL = "Update '"
. $ Table
. ''Set'. implode (',', $ SET)
. ($ Where )? "WHERE $ where ":'');
$ Stmt = $ this-> query ($ SQL, array_values ($ bind ));
$ Result = $ this-> affectedRows ();
Return $ result;
}
Public function delete ($ table, $ where = '')
{
/**
* Build the DELETE statement
*/
$ SQL = "DELETE FROM"
. $ Table
. ($ Where )? "WHERE $ where ":'');
/**
* Execute the statement and return the number of affected rows
*/
$ Stmt = $ this-> query ($ SQL );
$ Result = $ stmt? Mysql_affected_rows ($ this-> conn): $ stmt;
Return $ result;
}
Public function close ()
{
$ This-> conn & mysql_close ($ this-> conn );
}
}
?>