Php Tutorial-detailed tutorial on using php to operate mysql

Source: Internet
Author: User
Php encapsulates all data operations (using an extension), and then provides operations for a database.

Php has two modes for database operations.

Mode 1: a type of database provides an operation method (php extension implementation)

Mode 2: php encapsulates all data operations (using an extension), and then provides operations for a database.

Mode 1: database-related

Mysql extension or mysqli (enhanced version of mysql ).

Take mysql extension as an example:

Mode 2: database abstraction layer

Pdo, odbc.

Mysql extension operation database.

Enable scaling.

See:

// Connection

$ Link = mysql_connect ('server address', 'username', 'password ');

The returned resource type is used to connect to the resource.

Equivalent:

Send operation request:

Basically, mysql_query (statement, connection identifier) is used,

Traditionally, the request is to select a database and set the character set.

Select database:

Mysql_select_db (database name );

Mysql_query ('use dbname ;');

Set character set:

Mysql_query ('set names utf8 ');

Process the returned results:

# P # mysql operations in php-processing returned results # e #

There are two types:

Some data is returned (such as the select statement and desc statement)

Whether the execution result is successful (such as insert and update)

The return values of the mysql_query () function are different depending on the returned results.

For example, if a select statement is executed, another resource is returned.

If an insert statement is executed, a Boolean value is returned.

If the Boolean type is returned, no additional processing is required. However, if mysql result is returned, we usually need to use the result set to obtain the data we need. Capture data from the result set:

Use the mysql_fetch_xxx series functions.

Note: the mysql result set also has the result set pointer concept (similar to the array pointer). Therefore, operations are usually performed on the elements pointed to by the result set pointer.

Among them, the mysql_fetch_xxx series functions can only obtain the information of the records pointed to by the current result set pointer. You can only get one at a time. However, after fetch is executed, the pointer can be automatically moved down. Therefore, if you need to obtain all the information in the result set, you need to use the loop process to traverse the result set.

Note: No matter how many records exist in the result set returned by mysql_query (), the structure is the same.

Close database connection:

If an error occurs, display or record the error message-error handling

By default, if an SQL statement error occurs, it is not found in the execution phase. In fact, errors have been generated, but mysql adopts the silent mode for error handling. no error is reported when errors are generated. Therefore, we need to determine whether the current SQL statement is successfully executed by using the return value of mysql_query. If the execution succeeds, the returned value is true or the result set (the resource type is considered to be true). If the execution fails, false is returned.

Mysql_error ();

Mysql_errno ();

Mysql_affected_rows ();

// Returns the number of affected rows. Insert, update, and delete records

Mysql_num_rows (result set); number of records in the result set.

Using the count (*) version: if engine = myisam, it is recommended to use count (*) for fast speed. Because the number of records has been recorded by myisam, return directly.

Mysql_insert_id (); obtain the auto_increment value generated by the last insert operation.

There are also a series of functions that you can use without using.

For example, mysql_list_dbs ()

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.