Key points for connecting MySQL to php and key points for mysqlphp

Source: Internet
Author: User

Key points for connecting MySQL to php and key points for mysqlphp

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:

1: ddl: Data Definition Language.

2: dml: Data Language (such as CURD );

3: dql: Data Query Language. (Such as select)

4: dtl: Data transaction language.

).

5: Receive and process the returned results.

6: disconnect.


The sample code is as follows:

<Pre name = "code" class = "php"> <? Php // 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 mysql_select_db ("test") to operate; // 3: send the SQL command mysql_query ("set names utf8"); // set the query encoding $ SQL = "select * from test1"; $ res = mysql_query ($ SQL, $ con ); // 4: returned results (returned by row traversal) while ($ row = mysql_fetch_row ($ res )) {echo "$ row [0] ------------- $ row [1] ----------- $ row [2] ----------- $ row [3] ----------- $ row [4]". '<br/>';} // 5: Release the resource 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.




Related Article

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.