Steps and methods for executing mysql Query in php

Source: Internet
Author: User
Tags mysql host
Steps and methods for executing mysql Query in php
This article introduces some methods for querying mysql databases in php and their differences. For more information, see.

Php to operate mysql:

1, $ connect = mysql_connect ('localhost', 'root', '000000') or die ('database connection failed. 'MySQL _ error (); link to mysql. 2. mysql_select_db ('database', $ connect) selects the connected database. 3, mysql_query ('set names gb2312 '); $ SQL = "select * from blog_article"; prepare the data to be queried. 4, $ datas = mysql_query ($ SQL); execute an SQL query. 5, $ data = mysql_fetch_assoc ($ datas) to obtain a piece of data cached in the memory. 6, print_r ($ data );

Similarities: all three functions return a row of data queried in the database ).

Difference: mysql_fetch_assoc () uses the key value (that is, the array subscript) of the corresponding field name in the database, for example, filed ['id'] = 1;

Mysql_fetch_row () uses the automatically generated number (generated from 0 in turn) as the key value (that is, the array subscript), for example, filed [0] = 1;

Mysql_fetch_array () uses the automatically generated number (generated from 0 in turn) as the key value (that is, the array subscript ), in addition, it also generates the corresponding field names in the database as the key value (that is, the array subscript) such as: filed [0] = 1, filed ['id'] = 1; that is, mysql_fetch_array () integrates the query results of mysql_fetch_assoc () and mysql_fetch_row.

Mysql_fetch_object () is similar to mysql_fetch_assoc. Only the array returned by mysql_fetch_assoc. Mysql_fetch_object () returns the object. Mysql_insert_id () obtains the ID generated by the previous INSERT operation. The value of a field in the returned result set returned by the mysql_result () function. The number of fields in the returned result set by the mysql_num_fields () function. Mysql_affected_rows (); returns the number of records affected by the previous MySQL operation. Mysql_num_rows (mysql_query ($ SQL) obtains 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. major differences: 1. when connecting, this function will first try to find a (persistent) connection that has been opened with the same user name and password on the same host. if it is found, the connection id is returned without a new connection. 2. second, after the script is executed, the connection to the SQL Server will not be closed. this connection will be enabled for future use (mysql_close () will not close the connection established by mysql_pconnect ).

Mysql_data_seek (mysql_query ($ SQL), 8); 8th pieces of data in the result set are obtained. (Mysql_num_rows (mysql_query ($ SQL) and mysql_data_seek (mysql_query ($ SQL), 8) cannot be used in mysql_unbuffered_query ($ SQL .) Mysql_unbuffered_query ($ SQL) is similar to mysql_query ($ SQL), but mysql_unbuffered_query ($ SQL) is not cached. Mysql_query ($ SQL) caches the query results. Mysql_close (); close the latest mysql link. Mysql_field_flags (mysql_query ($ SQL), 6) returns the table attribute output of the sixth field, for example, 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 where the specified field is located. The mysql_free_result (mysql_query ($ SQL) function releases the result memory. The mysql_get_client_info () function returns the MySQL client information. Mysql_get_host_info () to obtain 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.