About PHP operation MySQL perform database query some common operations summary _php tips

Source: Internet
Author: User

PHP operation MySQL Steps:
1. $connect =mysql_connect (' localhost ', ' root ', ' 123456 ') or Die (' database connection failed. ' Mysql_error (); link mysql.
2.mysql_select_db (' database ', $connect) select the linked database.
3.mysql_query (' Set names gb2312 '); $sql = "SELECT * from Blog_article"; Prepare the data to be queried.
4. $datas = mysql_query ($sql); Execute SQL query.
5. $data = MYSQL_FETCH_ASSOC ($datas) Gets the data cached in memory in the query.
6.print_r ($data);

Same point:All three functions return a single row of data that is queried in the database (one more point is a piece of data).
different points:MYSQL_FETCH_ASSOC () uses the corresponding field name in the database as the key value (that is, the array subscript)
such as: filed[' id ']=1;
Mysql_fetch_row () is a key value (that is, an array subscript) that automatically generates numbers (which are generated sequentially from 0).
such as: filed[0]=1;
Mysql_fetch_array () uses the automatically generated number (which is generated from 0) as the key value (that is, the array subscript), and it also generates the corresponding field name in the database as the key value (that is, the array subscript)
Such as:
filed[0]=1,filed[' id ']=1 in other words, mysql_fetch_array () combines the results of MYSQL_FETCH_ASSOC () and mysql_fetch_row () queries into one.
Mysql_fetch_object () is about the same as MYSQL_FETCH_ASSOC (). Just Mysql_fetch_assoc () returns an array. Mysql_fetch_object () returns an object.
MYSQL_INSERT_ID () Gets the ID generated by the previous insert operation.
The mysql_result () function returns the value of a field in the result set.
The Mysql_num_fields () function returns the number of fields in the result set.
Mysql_affected_rows (); Returns the number of rows of records affected by the previous MySQL operation.
Mysql_num_rows (mysql_query ($sql)) Gets the number of rows in the result set.
The Mysql_pconnect () function opens a persistent connection to the MySQL server.

Mysql_pconnect () and mysql_connect () are very similar, but there are two main differences:
1. When connecting, this function will first attempt to find a (persistent) connection that has been opened with the same username and password on the same host, and if found, returns this connection identity without opening a new connection.
2. Second, when the script completes the connection to the SQL server is not closed, the connection remains open for later use (Mysql_close () does not close the connection established by Mysql_pconnect ()).
Mysql_data_seek (mysql_query ($sql), 8); Obtain the 8th data in the result set. (Mysql_num_rows (mysql_query ($sql)) and Mysql_data_seek (mysql_query ($sql), 8) are not available in Mysql_unbuffered_query ($sql). )
Mysql_unbuffered_query ($sql) and mysql_query ($sql) have the same effect, but
Mysql_unbuffered_query ($sql) is not cached. mysql_query ($sql) caches the results of the query.
Mysql_close (); Close the closest link to MySQL.
Mysql_field_flags (mysql_query ($sql), 6) Returns the Table property output for the sixth field, such as: Not_null Primary_key auto_increment.
Mysql_fetch_lengths (mysql_query ($sql)) returns the length of each field in all fields of the data. Returns an array of numbers.
Mysql_field_name (mysql_query ($sql), 3) Returns the field name of the third field.
Mysql_field_table (mysql_query ($sql), 0) returns the name of the table that contains the specified field.
The Mysql_free_result (mysql_query ($sql)) function frees the resulting memory.
The Mysql_get_client_info () function returns the MySQL client information.
Mysql_get_host_info () Gets the MySQL host information.

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.