Universal MySQL Database connection class code

Source: Internet
Author: User
Tags mysql query mysql database

Database connectivity is a limited and expensive resource, and database connections affect the performance metrics of the program. The database connection pool is proposed for this problem. The database connection pool is responsible for allocating, managing, and freeing the database connection, which allows the application to reuse an existing database connection without having to re-establish a database connection that frees up idle time beyond the maximum idle time to avoid missing the database connection caused by not releasing the database connection. This technique can significantly improve the performance of database operations.


/*
* Created on 2010-3-8
* Make By:suniteboy
* My-MySQL class
*
*/

Class mysql{
Private $server = "";
Private $user = "";
Private $pwd = "";
Private $database = "";
Private $linkmode = 1; Connection mode, 0 for normal connection, 1 for permanent connection
Private $conn = 0;
Private $sql = ""; SQL statement
Private $result = ""; Query results
Private $record = ""; Save 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, please check if the server address, user name, and connected database information is provided");
   return 0;
  }
   $this->server = $server;
   $this->user = $user;
   $this->pwd = $pwd;
   $this->database = $database;
   $this->charset = $charset;
   $this->linkmode = $linkmode;
   $this->connect ();
 }

//============================================
Connection functions
//============================================
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 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 empty ');
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);
}
//============================================
Gets the number of rows that were 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 ();
 }
 //========== ==================================
 //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;
  
 

 //============================================
 //Remove 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;
   &NBSP}
   return $arr;
  
  else
  {
    return false;
  }
 }

//============================================
Error prompt 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 "</br>";
}
ElseIf ($sql && $msg)
{
Echo $msg;
echo "</br>";
Echo $sql;
}
Else
{
Echo $msg;
echo "</br>";
}

}

}

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.