PHP mysql_connect and Mysql_pconnect functions and examples Tutorial _php tutorial

Source: Internet
Author: User
Tags mysql tutorial
When connecting, this function will first attempt to find a (persistent) connection with the same user name and password on the same host, and if found, return this connection identity without opening a new connection. Second, when the script finishes executing the connection to the SQL server is not closed, the connection will remain open for later use (Mysql_close () will not close the connection established by Mysql_pconnect ().

PHP tutorial MySQL tutorial _connect and mysql_pconnect functions and examples tutorial

Mysql_connect

Mysql_connect ($this->root, $this->user, $this->pass)

/*

Mysql_connect, a single rhetorical question user does not frequently call the database tutorial, there is no need to keep the connection, and MySQL connection number is also limited, use timely access is more frequent, it is best to use mysql_connect, so that the use of resources can be released immediately, otherwise, Easily lead to resource consumption
*/
Mysql_pconnect
/*
The Mysql_pconnect () function opens a persistent connection to the MySQL server
*/

$con = mysql_pconnect ("localhost", "Mysql_user", "mysql_pwd");
if (! $con)
{
Die (' Could not connect: '. Mysql_error ());
}


Class testlinkmysql{
public $conn;
Public $root = ' localhost ';
Public $user = ' root ';//' Loupan ';
Public $pass = ' root ';//' Loupandsffds ';
Public $db = ' dbname ';
Public $charset = ' GBK ';
Public $links = ' C '; Title

function __construct () {
if (! $this->conn)
{
$this->connect ();
}
}


function __destruct () {
if ($this->conn)
{
$this->close ();
}
}


function Mysqlconnect ()
{
try{
if (' p ' = = $this->links)
{
$this->conn = mysql_pconnect ($this->root, $this->user, $this->pass) or Die (Mysql_error ());
}
Else
{
$this->conn = mysql_connect ($this->root, $this->user, $this->pass) or Die (Mysql_error ());
}
mysql_select_db ($this->db, $this->conn);
mysql_query ("Set Names ' $this->charset '");
}catch (Exception $e) {
Echo ' Database connection failed, please contact the relevant personnel! ';
Exit
}
}

function Close ()
{
Mysql_close ($this->links);
}
}



/*
Summarize:
Mysql_pconnect () and mysql_connect () are very similar, but there are two main differences:

When connecting, this function will first attempt to find a (persistent) connection with the same user name and password on the same host, and if found, return this connection identity without opening a new connection.
Second, when the script finishes executing the connection to the SQL server is not closed, the connection will remain open for later use (Mysql_close () will not close the connection established by Mysql_pconnect ().
This site original tutorial reproduced annotated source http://www.bKjia.c0m/phper/php.html

http://www.bkjia.com/PHPjc/630873.html www.bkjia.com true http://www.bkjia.com/PHPjc/630873.html techarticle when connecting, this function will first try to find a (persistent) connection that has been opened with the same user name and password on the same host, and if found, returns this connection ID instead of ...

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