PHP operation MySQL database function (Tianlong eight)

Source: Internet
Author: User

===================================
Operation of the Php_mysql
===================================
First, the operation steps:
1. Connect to MySQL database
2. Determine if the connection is successful
3. Select a database
4. Setting the character set
5. Preparing SQL statements
6. Send SQL statements to the MySQL service
7. Parse the result set
8. Release the result set and close the database connection

Second, common operation:
1.mysql_connect ();--Connect to the database and return a connection resource
Format: mysql_connect (host name, user, password);
--where parameters are optional, if not written, refer to the default configuration in php.ini

2.mysql_error (); --Get the error message just (last) to perform the database operation

3. Mysql_errno (); --Get the error number that just (last) performed the database operation
Error number 0 means no error

4.mysql_select_db(database name [, database connection]);
Select a database, equivalent to the Use library name statement

5.Mysql_set_charset(character encoding);--Set character encoding
For example: Mysql_set_charset ("UTF8"); Equivalent to: mysql_query ("Set names UTF8");

6.mysql_query(SQL statement [, database connection]);--Send an SQL statement
If the SQL statement is queried, the result set is returned, and the other returns a Boolean value indicating whether the execution was successful.

7. Parse the result set function:
mysql_fetch_array (); --Parse the result set in an array of associative and indexed ways
You can also specify a second parameter to define the return format:
Mysql_both (0 associations and indexes)/mysql_num (2 index)/MYSQL_ASSOC (1 associations)
            
mysql_fetch_row (); --Parse the result set in an indexed array
*mysql_fetch_assoc (); --parse result set with associative array
mysql_fetch_object (); --Parse the result set in object mode

8.Mysql_free_result(Result set name);--release result set

9.Mysql_close(database connection); --Close database connection

10.mysql_num_rows(result set); --Get the number of data bars in the result set

11.Mysql_num_fields(result set); --Get the number of columns in the result set (number of fields)

12.mysql_result (); --Positioning results
Echo mysql_result ($result, 0, 3). " <br/> "; Gets the value in the 4th column of the 1th piece of data
Echo mysql_result ($result,.). " <br/> "; Gets the value in the 3rd column of the 2nd piece of data
Echo mysql_result ($result, 5,4). " <br/> "; Gets the value in the 5th column of the 6th piece of data

13.mysql_affected_rows-Get the number of record rows affected by the previous MySQL operation
The number of record rows affected by the associated insert,update or DELETE query.

14.mysql_insert_id-Get the ID generated by the INSERT operation in the previous step









PHP operation MySQL database function (Tianlong eight)

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.