How to use PHP to connect to MySQL data _ PHP Tutorial

Source: Internet
Author: User
Key points for connecting PHP to MySQL data. The following describes how to use the MySQL extension library to connect to MySQL data in PHP: 1. obtain the connection. 2. select the library. 3: Set the operation encoding. 4: sending SQL commands (key points for connecting MySQL data using MySQL PHP

To operate a MySQL database, follow these steps:

1: Get the connection.
2: Select the library.
3: Set the operation encoding.
4: send SQL commands (MySQL database can be divided into four commands:

4.1: ddl: Data Definition Language.
4.2: dml: Data language (such as CURD );
4.3: dql: data query language. (Such as select)
4.4: dtl: Data Transaction Language.

5: receive and process the returned results.
6: disconnect.

The sample code is as follows:

The code is as follows:


// 1: connect to the database
$ Con = mysql_connect ("localhost", "root", "toor ");
// If no connection is successful, an error is returned.
If (! $ Con ){
Echo "connection failed ";
Exit ();
}
// 2: Select the database to operate
Mysql_select_db ("test ");
// 3: send SQL commands
Mysql_query ("set names utf8"); // sets the query encoding.
$ SQL = "select * from test1 ";
$ Res = mysql_query ($ SQL, $ con );
// 4: return results (returned by row traversal)
While ($ row = mysql_fetch_row ($ res )){
Echo "$ row [0] ------------- $ row [1] ----------- $ row [2] ----------- $ row [3] ----------- $ row [4]".'
';
}
// 5: release resources and close the connection
Mysql_free_result ($ res );
Mysql_close ($ con );
?>

It should be noted that all operations here are performed in the memory and cannot be taken for granted, understanding some underlying things has some benefits.

Follow these steps to operate the MySQL database using the MySQL extension Library: 1: obtain the connection. 2: Select the library. 3: Set the operation encoding. 4: send SQL commands (MySQL count...

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.