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...