Common PHP database operation methods (MYSQL version) _ PHP Tutorial

Source: Internet
Author: User
Tags php database
Common PHP database operations (MYSQL ). 1. database operation 1. connect to MYSQL data mysql_connect () e.g. copy the code as follows: $ dbmysql_connect (MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) ordie ('unabletoconnect, I. database operations
1. connect to MYSQL data
Mysql_connect ()
E.g.

The code is as follows:


$ Db = mysql_connect (MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or die ('unable to connect, please check connection paremeters ');


2. select a database
Mysql_select_db ()
After connecting to the database, the default database selected by PHP may not be the database we need in subsequent operations. to ensure that the database is correctly selected, the database selection statement is usually added after the database connection statement.
E.g.

The code is as follows:


Mysql_select_db (MYSQL_DB, $ db) or die (mysql_error ($ db ));


3. execute SQL statements
Mysql_query ()
This function sends the SQL statement to the active database and executes the statement to return the result.
E.g.

The code is as follows:


$ Query = "SELECT * FROM $ table"
$ Result = mysql_query ($ query, $ db) or die (mysql_error ($ db ));


4. shut down the database
Mysql_close ()
This function is used to shut down the database that does not need to continue to be active, but this method is not required. generally, PHP automatically closes the database that does not continue to be active.
E.g.
Mysql_close ($ db );
5. release SQL results
Mysql_free_result ()
This function is used to release the memory occupied by mysql_query () execution results. This function is rarely called unless the result is large and occupies too much memory; generally, the occupied memory is automatically released after the PHP script is executed.
II. SQL execution result operations
1. return a row in the execution result
Mysql_fetch_row ()
Returns the number array of the current row of the execution result. after this function is executed, the result points to the next row.
E.g.
$ Row = mysql_fetch_row ($ result );
Processing execution results are generally placed in a while loop, traversing each row
E.g.
While ($ row = mysql_fetch_row ($ result ))
{......}
2. alternative to mysql_fetch_row ()
Mysql_fetch_array ()
Mysql_fetch_assoc ()
Mysql_fetch_array () returns the key-value pair array. The key is the column name of the queried table;
Mysql_fetch_assoc () can be sorted first when the returned results are returned (if an optional parameter value is assigned), which is equivalent to mysql_fetch_array () + MYSQL_ASSOC
3. Field (column) attributes of execution results
Mysql_fetch_field ()
4. query the table name in the database
Mysql_list_tables ()
E.g.

The code is as follows:


$ Db_name = MYSQL_DB;
$ Result = mysql_list_tables ($ db_name );
Echo "the database contains the following table :";
While ($ row = mysql_fetch_row ($ result ))
{
Echo $ row [0];
}


5. query the database column name (field name)
Mysql_list_fields ()
E.g.

The code is as follows:


$ Fields = mysql_list_fields ($ db_name, $ table );
$ Columns = mysql_num_fields ($ fields );
For ($ I = 0; $ I <$ columns; $ I ++)
Echo mysql_field_name ($ fields, $ I );


III. Other functions
1. mysql_num_rows ()
Number of rows returned for execution results.
E.g.
$ Num = mysql_num_rows ($ result );
2. mysql_num_fields ()
Number of columns (number of fields) returned for execution results ).
E.g. $ num = mysql_num_fields ($ result );
3. mysql_set_charset ()
Sets the encoding of execution results to prevent garbled characters when Chinese characters are displayed on the webpage.
E.g.

The code is as follows:


$ Query = "select * from $ table_name ";
Mysql_query ('set names utf8 ′);
$ Result = mysql_query ($ query, $ db) or die (mysql_error ($ db ));


Note:
1. the capital code in the text is pre-defined content, such as define (MYSQL_HOST, 'localhost ');
2. this article only summarizes the main functions of PHP database operations. for the complete content, see the relevant content of the PHP Manual.

Connector 1. connect to MYSQL data mysql_connect () e.g. the code is as follows: $ db = mysql_connect (MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or die ('unable to connect ,...

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.