1. Database operation
1. Connect MYSQL data
mysql_connect()
e.g.
Copy code 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 the database
mysql_select_db()
After connecting to the database, the database selected by PHP by default may not be the database we need in subsequent operations. To ensure that the database is selected correctly, a database selection statement is usually added after the database connection statement. .
e.g.
Copy code The code is as follows:
mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db ));
3. Execute SQL statement
mysql_query()
This function sends the SQL statement to the currently active database and executes the statement, returning the result.
e.g.
Copy code The 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 the database and does not need to continue Active database, but this method is not required. Generally, PHP will automatically close databases that are no longer active.
e.g.
mysql_close($db);
5. Release SQL results
mysql_free_result()
This function is used to release the memory occupied by the execution result of mysql_query(). This function is rarely called , unless the result is very large and takes up too much memory; usually the occupied memory is automatically released after the PHP script is executed.
2. SQL execution result operations
1. Return a row in the execution result
mysql_fetch_row()
Return the numerical array of the current row of the execution result. After executing this function, The result points to the next line.
e.g.
$row = mysql_fetch_row($result);
Processing execution results is generally placed in a while loop, traversing each row
e.g.
while($row = mysql_fetch_row($result))
{……}
2. Alternatives to mysql_fetch_row()
mysql_fetch_array()
mysql_fetch_assoc()
mysql_fetch_array() returns an array of key-value pairs, where the key is the column name of the queried table ;
Mysql_fetch_assoc() can sort the results first (if assigned to optional parameters), which is equivalent to mysql_fetch_array()+MYSQL_ASSOC
3. Field (column) attributes of the execution result
mysql_fetch_field()
4. Query the table name in the database
mysql_list_tables()
e.g.
Copy the code The code is as follows:
$db_name = MYSQL_DB;
$result = mysql_list_tables($db_name);
echo "The database contains the following tables:";
while ($row = mysql_fetch_row($result))
{
echo $row[0];
}
5. Query the column name (field name) of the database
mysql_list_fields()
e.g.
Copy code 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);
3. Other functions
1. mysql_num_rows()
Returns the number of rows in the execution result.
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()
Set the encoding of the execution result to prevent garbled characters when displaying Chinese on the web page.
e.g.
Copy code 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 uppercase codes in the article are predefined content, such as define(MYSQL_HOST, 'localhost');
2. This article only summarizes the main functions of PHP operating database, please refer to the complete content Related content of the PHP manual.
http://www.bkjia.com/PHPjc/323595.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323595.htmlTechArticle1. Database operation 1. Connect MYSQL data mysql_connect() e.g. Copy the code as follows: $db = mysql_connect(MYSQL_HOST , MYSQL_USER, MYSQL_PASSWORD) or die('Unable to connect,...