MySQL extension in PHP: common extension functions used by PHP to access MySQL _ MySQL

Source: Internet
Author: User
Tags field table
Source: http: wwwido321com1024html 1. PHP connects to the database and basic MySQL operations adopt the 'client server' architecture. Using MySQL extension functions installed in PHP, and directly using the client software area to access the MySQL database server, principle Source: http://www.ido321.com/1024.html

I. PHP database connection and basic operations

MySQL adopts the 'client/server' architecture. Using the MySQL extension function installed in PHP is the same as directly accessing the MySQL database server using the client software zone. in the same principle, you must send an SQL command to the MySQL management system and then return the result to the user.

In PHP, SQL is classified into two types (view SQL statements): one is a DQL statement with a returned result set, such as the select/desc table name. after execution, PHP is required to process the result set. Second, there is no result set, such as DML and DDL,However, after the DML statement is successfully executed, the data table records are affected.

 0) {// mysql_insert_id () returns the auto_increment value of the last new record echo 'data inserted successfully'. mysql_insert_id ().'
';} Else {echo' failed to insert data, error code: '. mysql_errno ().' error message: '. mysql_error ().'
';} // Close the connection mysql_close ($ link);?>

II. PHP processes the select query result set

Execute the select statement in PHP to return a result set, which can be used to process each field

$ Result = mysql_query ('select * from test'); // Obtain the number of record rows $ rows = mysql_num_rows ($ result); // Obtain the number of fields, that is, the data column $ cols = mysql_num_fields ($ result );

If you need to access data in the result set, you can use one of the following four functions (the resource operator of the result set is used as a parameter, and the next record is automatically returned, and false is returned at the end of the table)

1. mysql_fetch_row (): This function returns a result record and stores it with a common index data.

2. mysql_fetch_assoc (): obtain a row from the result set and save the row as the associated data.

3. mysql_fetch_array (): Get a row from the result set as an associated array, or a number array, or both. You can use MYSQL_ASSOC (associated array form), MYSQL_NUM (index array form), and MYSQL_BOTH as the second parameter to specify the returned data form.

4. mysql_fetch_object (): obtains a row from the result set as an object, and each field is accessed as an object.

Suggestion: there are no special requirements. do not use mysql_fetch_array (). you can use mysql_fetch_row () or mysql_fetch_assoc () to implement the same function and improve efficiency.

There are also three common functions related to the result set.

5. mysql_data_seek (int $ num): move the internal result Pointer. $ num is the number of rows of the new result set pointer to be set.

6. mysql_fetch_lengths (resource $ result): Gets the length of each output in the result set.

7. mysql_result (resource $ result, int $ row [, mixed $ field]): return the content of a unit in the MySQL result set. The field parameter can be the offset or field name of the field, or tablename. fieldname of the field table ). If an alias ('select foo as bar from... '), The column name is replaced by an alias. CallMysql_result ()It cannot be called together with functions in other processing result sets.

Next Article: how to use WordPress to create a custom registry single plug-in

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.