For more information about a php Mysql class, see.

Source: Internet
Author: User
Tags pconnect

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.