PHP connection MySQL Database

Source: Internet
Author: User

Class classdb{
var $localhost = ' 127.0.0.1 ';
var $user = ' root ';
var $pass = ' 123456 ';
var $links = ';
var $charset = ' GBK ';

Function Connect ()
{
try{
if (' p ' = = $this->links)
{
$this->conn = mysql_pconnect ($this->localhost, $this->user, $this->pass) or Die (Mysql_error ());
}
Else
{
$this->conn = mysql_connect ($this->locahost, $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, ';
Exit
}
}
}

Code Instance Two


mysql_connect ("localhost", "root", "1981427")//Select database before you need to connect to the database server
Or Die ("Database server Connection Failed");
mysql_select_db ("test")//Select Database MyDB
Or Die ("database does not exist");

Starter Database Connection Two

Mysql_connect ("$host", "$user", "$password") or Die ("Unable to connect to the MySQL database server!") ");

$db = mysql_select_db ("$dbname") or Die ("Unable to connect to the database!") ");


/*
Description
Mysql_connect (Host: port, username, password); the MySQL server connected to the batch defaults to 3306 ports, the connection is closed automatically when the script is finished, or it can be closed in advance with Mysql_close
mysql_create_db (database name); Select default Database
mysql_query (SQL statement, connection number), querying the specified database

This article is original in www.111cn.net reprint annotated provenance, otherwise must investigate

*/

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.