Php connection to mysql database test instance-PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php connection to the mysql database test instance. If you want php to connect to mysql, we need to know that php needs to be in php. the mysql module is enabled in ini. the specific enabling method is described later, next let's look at php connection my If you want php to be able to connect to mysql our homepage should know that php needs to be in php. the mysql module is enabled in ini. the specific enabling method will be introduced later. let's take a look at the php connection to mysql instance testing.

PHP connects to the MySQL database through the mysql_connect () function to open a non-persistent MySQL connection.
Syntax:
Mysql_connect (servername, username, password );

Parameter description:
Servername: Optional. The name of the server to be connected. the default value is "localhost: 3306". generally, enter localhost.
Username: Optional. The username used to log on to the database server is generally root.
Password: Optional. The password used to log on to the database server.


Example:

The code is as follows:

Header ("Content-type: text/html; charset = utf-8 ");
$ Link_id = @ mysql_connect ('localhost', 'root', '123 ');
If (! $ Link_id ){
Die ('server connection failed ');
}
If (! @ Mysql_select_db ('web', $ link_id )){
Die ('database connection failed ');
}
If (! @ Mysql_query ("set names 'utf8'", $ link_id )){
Die ('failed to set utf8 format ');
}

Mysql_close ();
?>


Example analysis:

Header ("Content-type: text/html; charset = utf-8 ");

Set the page content to html and the page encoding format to UTF-8.
Ensure: 1. database encoding 2. page encoding 3. connection encoding are consistent, so no garbled characters will occur.

The code is as follows:

$ Link_id = @ mysql_connect ('localhost', 'root', '123 ');

If the connection is successful, a MySQL connection id is returned to $ link_id. if the connection fails, FALSE is returned. @ Is to prevent leakage of website privacy by not displaying database error information.

The code is as follows:

If (! $ Link_id ){
Die ('server connection failed ');
}

Check whether the database server is connected successfully. if the connection fails, output "connection to the server failed" and terminate php execution.

The code is as follows:


If (! @ Mysql_select_db ('web', $ link_id )){
Die ('database connection failed ');
}

Check whether the connection to the server database is successful. if the connection fails, output "connection failed" and terminate php execution.

The code is as follows:


If (! @ Mysql_query ("set names 'utf8'", $ link_id )){
Die ('failed to set utf8 format ');
}

Set the encoding of the php connection to the mysql database. if the code fails, the output information "failed to set the utf8 format" and terminate php execution.

The code is as follows:

Mysql_close ();

Release resources, that is, close the database.

Mysql database connection prompt

The following code appears: Call to undefined function 'MySQL _ connect ()'... Failed

Baidu found that the PHP + MYSQL environment is not configured, and php5's default mysql is disabled.

Copy the php_mysql.dll and libmysql. dll files to c:/winnt/system32 (I missed libmysql. dll)

Find; extension = php_mysql in php. ini and remove ";" restart the server.

...

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.