CopyCode The 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 );
}
}
?>