General MySQL database connection Class Code _php tutorial

Source: Internet
Author: User
Tags mysql query mysql tutorial
General MySQL Tutorial database Tutorial Connection Class code

Database connections are a limited and expensive resource, and database connectivity affects the performance metrics of the program. The database connection pool was raised for this issue. The database connection pool is responsible for allocating, managing, and freeing the database connection, which allows the application to reuse an existing database connection instead of re-establishing a database connection that has been idle for more than the maximum idle time to avoid missing database connections due to the absence of a database connection being freed. 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 for normal connection, 1 for permanent connection
Private $conn = 0;
Private $sql = ""; SQL statements
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, check that the server address, user name, and database information for the connection 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 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 record 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 ();
}
//============================================
Take a result out of 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;
}
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 "
";
}
ElseIf ($sql && $msg)
{
Echo $msg;
echo "
";
Echo $sql;
}
Else
{
Echo $msg;
echo "
";
}

}

}

http://www.bkjia.com/PHPjc/630850.html www.bkjia.com true http://www.bkjia.com/PHPjc/630850.html techarticle General MySQL Tutorial database Tutorial connection Class code database connections are a limited and expensive resource, and database connections affect the performance metrics of the program. Database connection pool is precisely 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.