[PHP]
- <span style="Font-family:arial,helvetica,sans-serif" > in PHP Operation Database, will frequently do various operations on the database, so, PHP provides a function interface, Support for a variety of databases, including MySQL database, is well supported. PHP function interface, in fact, is the placement of SQL statements </span>
1. Database connection to use the database, the first need to connect to the database, the link command for
[PHP]
- Mysql_connect (servername, user, password);
servername is the server name, optional, default is localhost:3306;User is the username , optionally, the user name of the server process is owned by default;pasword rules The password used for login, optional, default is "";
[PHP]
- <?PHP&NBSP;&NBSP;
- $con =mysql ( "localhost", "root",
- if ( $con)
- {
- < span class= "keyword" >die ( "couldn ' t connect!");
- }
- //the end of use, close the link
- mysql_ Close ( $con);
- ?>&NBSP;&NBSP;
You can also use Mysql_pconnect ([host][:p Ort][user][password]), which is basically the same function as the mysql_connect function, except that:
1. When the database operation is finished, the connection established by the Mysql_connect function is automatically closed, and the connection established by the Mysql_pconnect function will continue to exist, which is a stable and persistent connection.
2. When connecting using the Mysql_pconnect function, the connection is checked for use of the same hostname, user, and password before each connection, and if so, the connection number is used directly.
3. The connection established by the function mysql_connect can be closed with the Mysql_close function, and the connection established by the Mysql_pconnect function cannot be closed with the Mysql_close function.
2. Transfer Query/Command mysql_query (query,connection) to MySQL to pass a query or command connection optional, if empty, to attempt to create a link with an empty argument.Assuming the query succeeds, you can call theMysql_num_rows ()To see how many rows were returned for the SELECT statement, or to callMysql_affected_rows ()To see how many rows are affected by the Delete,insert,replace or UPDATE statement.only the Select,show,describe or EXPLAIN statement mysql_query () returns a new result identifier that can be passed to the mysql_fetch_array () and other functions that process the result table. After processing the result set , the associated resource can be freed by calling Mysql_free_result () , although the script will automatically free memory after execution, and false if it is executed incorrectly. Returns true for other executions, and returns false without execution.
3. Select a database
4. Return result processing function 1) mysql_num_rows () (query result pointer) returns the number of rows in the query result set. 2) Mysql_affected_rows () returns the number of record rows (for UPDATE, delete, insert, replace) that the MySQL operation affects on the database 3) mysql_num_fields () (query result pointer) Returns the number of fields in the query result set. 5. Traverse query Recordset Mysql_fetch_array (), Mysql_fetch_row, Mysql_fetch_assoc difference: All three can fetch a row of records from the result set to return. The results of mysql_fetch_array () extraction can be accessed either by index or by keyword, whereas the results of mysql_fetch_row extraction can only be accessed through the keyword by means of index access, MYSQL_FETCH_ASSOC extraction. 6. Error handling function 1) mysql_errno (typeface identifier) returns the error message in the previous MySQL operation number 2) Mysql_error () returns the text error message for the previous MySQL operation.
7.web Database Principle Browser <----> Server <------>php Engine <-------The >mysql database process is as follows: 1) The user relaxes an HTTP request to the server via the browser, requesting a Web file ; 2) The server receives a Web request, obtains the file, and transmits the Web file to the PHP engine; 3) PHP begins parsing the script with a SQL query that contains the command and query statements for the linked database, PHP opens the connection to MySQL, and sends the query ; 4) The database accepts queries and processes, passes the results of the query to the PHP engine, 5) The PHP engine accepts the results, completes the script, and outputs the HTML;6) the server sends the HTML format file to the browser.
from PHP to MySQL query, PHP's mysql_query () function only starts the transfer function, not the main access instruction operation. PHP is not a built-in operation for MySQL, only SQL commands are used to manipulate the database. mysql_query () can pass various SQL syntax.
PHP Operation MySQL Database