Common PHP Database Operation method (MySQL version) _php tutorial

Source: Internet
Author: User
first, the database operation
1. Connect MySQL Data
Mysql_connect ()
e.g.
Copy CodeThe code is as follows:
$db = mysql_connect (Mysql_host, Mysql_user, Mysql_password) or Die (' unable-connect, please check connection paremeters ' );

2. Select a database
mysql_select_db ()
After connecting to the database, PHP default selection of the database is not necessarily the database we need in the subsequent operations, in order to ensure that the database selection is correct, usually after the database connection statement also add the database SELECT statement.
e.g.
Copy CodeThe code is as follows:
mysql_select_db (mysql_db, $db) or Die (Mysql_error ($db));

3. Execute SQL statements
mysql_query ()
The function sends the SQL statement to the currently active database and executes the statement, returning the result.
e.g.
Copy CodeThe code is as follows:
$query = "SELECT * FROM $table"
$result = mysql_query ($query, $db) or Die (Mysql_error ($db));

4. Close the database
Mysql_close ()
This function is used to close a database that does not need to continue to be active, but this method is not required, and generally PHP automatically shuts down databases that do not continue to be active.
e.g.
Mysql_close ($DB);
5. Releasing SQL results
Mysql_free_result ()
This function frees the memory occupied by the mysql_query () execution result, which is seldom called unless the result is very large and takes up too much memory; it usually frees up memory after the execution of the PHP script.
ii. SQL Execution result operation
1. Return a row in the execution result
Mysql_fetch_row ()
Returns an array of values for the current row of the execution result, after which the result points to the next line.
e.g.
$row = Mysql_fetch_row ($result);
Processing execution results are typically placed in a while loop, traversing each row
e.g.
while ($row = Mysql_fetch_row ($result))
{......}
2. Alternative methods for Mysql_fetch_row ()
Mysql_fetch_array ()
MYSQL_FETCH_ASSOC ()
Mysql_fetch_array () returns an array of key values, the column name of the table to which the key is queried;
MYSQL_FETCH_ASSOC () The result can be sorted first (if an optional parameter is assigned), which is equivalent to mysql_fetch_array () +mysql_assoc
3. Field (column) Properties for execution results
Mysql_fetch_field ()
4. Querying the table names in the database
Mysql_list_tables ()
e.g.
Copy CodeThe code is as follows:
$db _name = mysql_db;
$result = Mysql_list_tables ($db _name);
echo "database contains the following table:";
while ($row = Mysql_fetch_row ($result))
{
echo $row [0];
}

5. Querying the Database for column names (field names)
Mysql_list_fields ()
e.g.
Copy CodeThe 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);

third, other functions
1. Mysql_num_rows ()
Returns the number of rows for which the result was executed.
e.g.
$num = mysql_num_rows ($result);
2. Mysql_num_fields ()
Returns the number of columns (number of fields) of the execution result.
e.g. $num = Mysql_num_fields ($result);
3.mysql_set_charset ()
Sets the encoding of the execution result to prevent garbled characters when displaying Chinese in a Web page.
e.g.
Copy CodeThe 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 uppercase code in the text is predefined content, such as define (Mysql_host, ' localhost ');
2. This article summarizes the main functions of PHP operation database, please refer to the PHP manual for complete contents.

http://www.bkjia.com/PHPjc/323595.html www.bkjia.com true http://www.bkjia.com/PHPjc/323595.html techarticle I. Database operations 1. Connect MySQL data mysql_connect () e.g. copy Code code as follows: $db = mysql_connect (Mysql_host, Mysql_user, Mysql_password) or di E (' 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.