PHP connection MySQL Database Test example detailed _php tutorial

Source: Internet
Author: User
If you want PHP to connect with MySQL our home page to know PHP needs to be in the php.ini MySQL module to open, the specific way to open the article will be introduced later, let's look at the PHP connection MySQL instance test it.

The PHP connection to the MySQL database is via the mysql_connect () function to open a non-persistent MySQL connection.
Grammar:
Mysql_connect (servername, username, password);

Parameter description:
ServerName: Optional. The name of the server to connect to, the default is "localhost:3306", generally fill out localhost.
Username: Optional. The user name that is logged into the database server is usually root.
Password: Optional. The password to log on to the database server.


Example:

The code is as follows Copy Code

Header ("content-type:text/html; Charset=utf-8 ");
$link _id = @mysql_connect (' localhost ', ' root ', ' 123456789 ');
if (! $link _id) {
Die (' Connection server failed ');
}
if (! @mysql_select_db (' web ', $link _id)) {
Die (' Connection database failed ');
}
if (! @mysql_query ("Set names ' UTF8 '", $link _id)) {
Die (' Set UTF8 format failed ');
}

Mysql_close ();
?>


Sample parsing:

Header ("content-type:text/html; Charset=utf-8 ");

Set page content is HTML, page encoding format is utf-8.
Guarantee: 1, Database Code 2, page encoding 3, the connection code is consistent, there will be no garbled phenomenon.

The code is as follows Copy Code

$link _id = @mysql_connect (' localhost ', ' root ', ' 123456789 ');

Connect to the database, and if successful, returns a MySQL connection identified to $link _id, and fails returns FALSE. @ is not output display database error information to prevent the disclosure of Web site privacy.

The code is as follows Copy Code

if (! $link _id) {
Die (' Connection server failed ');
}

To determine if the database server is successful or unsuccessful, the output message "Failed to connect to the server" and terminated the execution of PHP.

The code is as follows Copy Code


if (! @mysql_select_db (' web ', $link _id)) {
Die (' Connection database failed ');
}

To determine if the connection server database is successful or unsuccessful, the output information "failed to connect to the database" and terminated the execution of PHP.

The code is as follows Copy Code


if (! @mysql_query ("Set names ' UTF8 '", $link _id)) {
Die (' Set UTF8 format failed ');
}

Set PHP connection MySQL database encoding, if unsuccessful, output information "Set UTF8 format failed" and terminate the execution of PHP.

The code is as follows Copy Code

Mysql_close ();

Frees the resource, that is: shuts down the database.

Connect to MySQL Database tips

Run code appears: Call to undefined function ' mysql_connect () ' ... Failed

Baidu found the result is Php+mysql environment is not configured well, php5 default MYSQL is off

Copy the Php_mysql.dll and Libmysql.dll files to the C:/winnt/system32 (I missed the Libmysql.dll)

Find the php.ini; extension=php_mysql, remove the front ";" Restart the server

http://www.bkjia.com/PHPjc/630688.html www.bkjia.com true http://www.bkjia.com/PHPjc/630688.html techarticle If you want PHP to connect with MySQL our home page to know that PHP needs to be in the php.ini MySQL module to open, the specific way to open the article will be introduced later, the following we see the PHP connection my ...

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