Http://www.cnblogs.com/cosiray/archive/2011/12/21/2295795.html
One, database operation 1. Connect MySQL Data
Mysql_connect ()
e.g.
$conn = mysql_connect ($HOST, $USER, $PASSWORD) or Die (Mysql_error ());
2. Select a database
mysql_select_db ()
After connecting to the MySQL database, select the specific database to use
e.g.
mysql_select_db ($database, $conn) or Die (Mysql_error ());
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.
$sql = "SELECT * FROM table";
$result = mysql_query ($sql, $database) or Die (Mysql_error ());
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 ($database);
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.
Second, SQL execution 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, 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. Query the database forTable name
Mysql_list_tables ()
e.g.
$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 DatabaseColumn Name(Field name)
Mysql_list_fields ()
e.g.
$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.
$query = "SELECT * from $table _name";
mysql_query (' Set names utf8′);
$result = mysql_query ($query, $db) or Die (Mysql_error ($db));