As a good partner in the lamp portfolio, PHP's connection to the MySQL database is an essential part of every program.
In previous versions of PHP 5.0, it was common to use the MySQL native extension function to connect, while the more powerful PDO (data abstraction layer) was recommended in later versions of 5.0.
Here's a quick summary of the common functions we typically need to interact with the database.
I. Configuring connection Database Information
1. Connect to the database
Mysql_connect ($server, $username, $password)
2. Setting the character set
Mysql_set_charset ($charset)
3. Select a database
mysql_select_db ($database _name)
Ii. Sending and executing SQL statements to the database
mysql_query ($sql);
SQL statement execution fails to return false, the execution succeeds if there are resources (such as execution of queries) to return the resource, the rest returns True.
Iii. parsing data from the returned result set
1. Returns the number of rows that affect the
Mysql_num_rows ($result) Gets the number of rows in the result set
Mysql_affected_rows () Gets the number of record rows affected by the previous operation
MYSQL_INSERT_ID (); Gets the ID that was generated by the previous step of the insert operation
2. Returns the query resource (the following is a one-row record from the result set)
Mysql_fetch_row () Digital Index
MYSQL_FETCH_ASSOC () field name index
Mysql_fetch_array () both have
3. Returns the error message
Mysql_error (); Text error message
Mysql_errno (); Numeric Encoding of error messages
Iv. Closing the database connection
Mysql_close ();
PHP connection MySQL primary MySQL function summary