Common PHP Database operation methods (MySQL version) _php skills

Source: Internet
Author: User
Tags mysql version php database php script

first, the database operation
1. Connect MySQL Data
Mysql_connect ()
e.g.

Copy Code code as follows:

$db = mysql_connect (Mysql_host, Mysql_user, Mysql_password) or die (' Unable to connect, please check connection paremeters ' );

2. Select Database
mysql_select_db ()
After connecting to the database, the PHP default selection of the database is not necessarily the database we need to do later, in order to ensure that the database selection is correct, generally after the database connection statement plus the database selection statement.
e.g.
Copy Code code as follows:

mysql_select_db (mysql_db, $db) or Die (Mysql_error ($db));

3. Execute SQL statement
mysql_query ()
The function sends the SQL statement to the currently active database and executes the statement, returning the result.
e.g.
Copy Code code 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 turn off databases that do not need to continue to be active, but this method is not required, and generally PHP automatically shuts down databases that are not active.
e.g.
Mysql_close ($DB);
5. Release SQL results
Mysql_free_result ()
This function is used to free memory that is consumed by the results of the mysql_query (), which is rarely invoked unless the result is large, consumes too much memory, and typically automatically frees up memory after the execution of the PHP script.
Second, the SQL performs the result operation
1. Returns a row in the execution result
Mysql_fetch_row ()
Returns an array of values for the current row of the execution result, which, after executing this function, points to the next line.
e.g.
$row = Mysql_fetch_row ($result);
Processing execution results are typically placed in the while loop, traversing each row
e.g.
while ($row = Mysql_fetch_row ($result))
{......}
2. Alternative method of Mysql_fetch_row ()
Mysql_fetch_array ()
MYSQL_FETCH_ASSOC ()
Mysql_fetch_array () returns the array of key values, and the key is the column name of the table on which the query is queried;
MYSQL_FETCH_ASSOC () can be sorted (if an optional parameter is assigned) when the result is returned, equivalent to Mysql_fetch_array () +mysql_assoc
3. Field (column) property of the execution result
Mysql_fetch_field ()
4. Querying the table name in the database
Mysql_list_tables ()
e.g.
Copy Code code as follows:

$db _name = mysql_db;
$result = Mysql_list_tables ($db _name);
The Echo 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.
Copy Code code 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 that performed the result.
e.g.
$num = mysql_num_rows ($result);
2. Mysql_num_fields ()
Returns the number of columns (fields) in which the result was executed.
e.g. $num = Mysql_num_fields ($result);
3.mysql_set_charset ()
Sets the encoding of the execution result to prevent garbled text when displayed in the Web page.
e.g.
Copy Code code 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 is a predefined content, such as define (Mysql_host, ' localhost ');
2. This article only summarizes the PHP operation database main function, the complete content please refer to the PHP manual related content.

Related Article

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.