Common mysql database connection code _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Common mysql database connection code. General mysql tutorial database tutorial connection class code database connection is a limited and expensive resource, database connection affects program performance indicators. The database connection pool is for this general mysql tutorial database tutorial connection class code

Database Connection is a limited and expensive resource. database connection affects program performance indicators. The database connection pool is designed to address this issue. The database connection pool is responsible for allocating, managing, and releasing database connections. it allows applications to repeat an existing database connection instead of reestablishing one; release the database connection with idle time exceeding the maximum idle time to avoid database connection omissions caused by the absence of the database connection. This technology can significantly improve the performance of database operations.


/*
* Created on 2010-3-8
* Make by: suniteboy
* My first mysql class
*
*/

Class mysql {
Private $ server = "";
Private $ user = "";
Private $ pwd = "";
Private $ database = "";
Private $ linkmode = 1; // connection mode. 0 indicates a normal connection, and 1 indicates a permanent connection.
Private $ conn = 0;
Private $ SQL = ""; // SQL statement
Private $ result = ""; // query result
Private $ record = ""; // save the record

// ================================================ ======
// Constructor
// ================================================ ======
Public function _ construct ($ server, $ user, $ pwd, $ database, $ charset = "utf8", $ linkmode = 0)
{
If (empty ($ server) | empty ($ user) | empty ($ database ))
{
$ This-> show_error ("The connection information is incomplete. check whether the server address, user name, and connected database information are provided ");
Return 0;
}
$ This-> server = $ server;
$ This-> user = $ user;
$ This-> pwd = $ pwd;
$ This-> database = $ database;
$ This-> charset = $ charset;
$ This-> linkmode = $ linkmode;
$ This-> connect ();
}

// ================================================ ======
// Connection function
// ================================================ ======
Public function connect ()
{
$ This-> conn = $ this-> linkmode? Mysql_pconnect ($ this-> server, $ this-> user, $ this-> pwd ):
Mysql_connect ($ this-> server, $ this-> user, $ this-> pwd );
If (! $ This-> conn)
{
$ This-> show_error ('unable to connect to server ');
Return 0;
}

If (! Mysql_select_db ($ this-> database ))
{
$ This-> show_error ('unable to connect to the database'. $ this-> database );
Return 0;
}
// $ This-> query ('set names'. $ this-> charset );
Return $ this-> conn;

}
// ================================================ ======
// Mysql Query function
// ================================================ ======
Public function query ($ SQL)
{
If (empty ($ SQL ))
{
$ This-> show_error ('SQL statement is Null ');
Return 0;
}
$ This-> SQL = preg_replace ('/{2,}/', '', trim ($ SQL ));
$ This-> result = mysql_query ($ this-> SQL, $ this-> conn );
If (! $ This-> result)
{
$ This-> show_error ('SQL statement error', true );
Return 0;
}
Return $ this-> result;
}

// ================================================ ======
// Function
// ================================================ ======
Public function select_db ($ dbname)
{
Return mysql_select_db ($ dbname );
}

Public function fetch_array ($ query, $ result_type = mysql_assoc)
{
Return mysql_fetch_array ($ query, $ result_type );
}

Public function fetch_row ($ query)
{
Return mysql_fetch_row ($ query );
}
// ================================================ ======
// Obtain the number of rows affected by the previous mysql operation
// ================================================ ======
Public function affected_rows ()
{
Return mysql_affected_rows ();
}
Public function num_fields ($ query)
{
Return mysql_num_fields ($ query );
}

Public function num_rows ($ query)
{
Return @ mysql_num_rows ($ query );
}

Public function insert_id ()
{
Return mysql_insert_id ();
}

Public function close ()
{
Return mysql_close ();
}
// ================================================ ======
// Retrieve a result from the record
// ================================================ ======
Public function getone ($ SQL)
{
$ Res = $ this-> query ($ SQL );
If ($ res! = False)
{
$ Row = mysql_fetch_row ($ res );
If ($ row! = False)
{
Return $ row;
}
Else
{
Return '';
}
}
Else
{
Return false;
}
}

// ================================================ ======
// Retrieve all results from the record
// ================================================ ======
Public function getall ($ SQL)
{
$ Res = $ this-> query ($ SQL );
If ($ res! = False)
{
$ Arr = array ();
While ($ row = mysql_fetch_assoc ($ res ))
{
$ Arr [] = $ row;
}
Return $ arr;
}
Else
{
Return false;
}
}

// ================================================ ======
// Error message function
// ================================================ ======
Public function show_error ($ msg = '', $ SQL = false)
{
$ Err = '['. mysql_errno (). ']'. mysql_error ();
If ($ SQL) $ SQL = 'SQL statement:'. $ this-> SQL;
If ($ msg = '')
{
Echo $ err;
Echo"
";
}
Elseif ($ SQL & $ msg)
{
Echo $ msg;
Echo"
";
Echo $ SQL;
}
Else
{
Echo $ msg;
Echo"
";
}

}

}

Database connection is a limited and expensive resource. database connection affects program performance indicators. The database connection pool is for this...

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.