|
|
楼主 |
发表于 2011-8-4 14:55:49
|
显示全部楼层
SQL Safe Mode
When the safe mode is turned on, database connect functions that specify default values will use those values in place of the supplied arguments. To enable the safe mode, edit the safe_mode parameter in php.ini as shown below.
Examples of valid $host values for MySQL:
- null
- localhost
- localhost:3306
- 127.0.0.1
- 127.0.0.1:3306
- :/tmp/mysql.sock
复制代码
The parameters $username and $password are the database username and the password respectively. In the safe mode, the name of the user that owns the server process and an empty password overrides the supplied username and the password.The method mysql_connect() can be used to reuse a connection to a MySQL server. If a second call is made to mysql_connect() with the same arguments, no new connection is established, but the connection handle of the already opened database connection is returned instead. Setting the $new_link parameter to TRUE modifies this default behavior and forces mysql_connect() to open a new database connection even if mysql_connect() was called before with the same parameters. In the safe mode this optional parameter is ignored.
The $client_flags parameter can be a combination of the following pre-defined constants:
- 128 - Enable LOAD DATA LOCAL handling.
- MYSQL_CLIENT_SSL - Use SSL encryption.
- MYSQL_CLIENT_COMPRESS - Use compression protocol.
- MYSQL_CLIENT_IGNORE_SPACE - Allow space after function names.
- MYSQL_CLIENT_INTERACTIVE - Allow interactive_timeout (instead of wait_timeout) seconds of inactivity before closing the connection.
In the safe mode, this optional parameter is ignored.
In the sample PHP script, the following code fragment connects to the MySQL server running on the localhost on the default port 3306 and returns a database connection handle.
- $host="localhost:3306";
- $user="root";
- $password="admin";
- $cxn = mysql_connect ($host, $user, $password)
- or die ('Could not connect to the database server' . mysql_error() )
复制代码
Persistent Database Connections
Persistent connections improve performance for some applications. When persistent connections are created from the application, the database connection is opened once and kept in pool for the application's lifetime. The PHP runtime tries to reuse the existing database connections from the pool whenever there is a request to connect to the database.
You can switch to persistent connections in MySQL by changing the function call from mysql_connect() to mysql_pconnect(). Both of those methods accept the same parameters.
| resource mysql_pconnect ([ string $server [, string $username [, string $password [, int $client_flags]]]] ) |
The call mysql_pconnect() establishes a persistent connection to a MySQL server. This method acts very much like mysql_connect() with two major differences:
- When connecting, the function first tries to find a persistent connection that is already open with the same host, username, and password. If it finds one, an identifier for it is returned instead of opening a new connection.
- The connection to the SQL server is not closed when the execution of the script ends. Instead, the connection remains open for future use. Note that calling mysql_close() does not close the database connection established by mysql_pconnect().
While persistent connections can improve performance for some applications, especially running over slow network links, they can lead to trouble if not used properly. For example, if you lock a table and close the database connection before unlocking it, normally it is unlocked when the connection is terminated. However because persistent connections do not close, any tables you accidentally leave locked will remain locked, and the only way to unlock them is probably to wait for the connection to time out or kill the process. Also due to the persistent nature of the connections, the process might consume more resources. To alleviate this issue, it might be a good idea to register a function that does an explicit rollback of non-committed transactions on script shutdown, with register_shutdown_function(). Check the Using persistent connections with MySQL page at mysql.com to learn more about the advantages and disadvantages of using persistent connections and decide for yourself whether or not to use persistent connections in your application.
Select the Database to Use
Once the database connection is established, the next step is to select any database on the MySQL server as the default (current) database for subsequent transactions. The mysql_select_db() method of ext/mysql can be used to select a MySQL database programmatically.
| bool mysql_select_db (string $database_name [, handle $link_identifier]) |
where $database_name is the name of the database that is to be selected, and $link_identifier is an optional parameter that represents the database connection handle. If the connection handle is not specified, by default, the last connection opened by mysql_connect() is assumed. If no such connection is found, it tries to create one as if mysql_connect() was called with no arguments.
The method mysql_select_db() returns TRUE on success and FALSE on failure. If the current active database is set successfully, every subsequent call to mysql_query() will be made against the active database.
In the sample code, the following code fragment tries to set test as the active database.
- $dbname="test";
- mysql_select_db ($dbname, $cxn)
- or die ("Unable to select database" . mysql_error());
复制代码
Execute the SQL Statement
Use the mysql_query() method of ext/mysql to execute repeated SELECT and non-SELECT statements. The query mysql_query() sends a unique query (multiple queries or Multiple Statements are not supported) to the currently active database on the server that is associated with the specified database connection handle.
| resultset mysql_query ( string $query [, handle $link_identifier]) |
where $query is a single SQL DDL or DML statement to execute without the terminating semicolon or \g, and $link_identifier is the optional parameter that represents the database connection handle. If the connection handle is not specified, by default, the last connection opened by mysql_connect() is assumed. If no such connection is found, it tries to create one as if mysql_connect() was called with no arguments.
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning a result set, mysql_query() returns a result set on success, or FALSE on error.
For other type of SQL statements like INSERT, UPDATE, DELETE, or DROP, mysql_query() returns TRUE on success or FALSE on error.
In the code sample, the following code fragment executes the SQL query.
- $query = "SELECT * FROM City";
-
- $result = mysql_query ($query, $cxn)
- or die ("Could not execute the query." . mysql_error());
- }
复制代码
Let's try to insert a few rows into the City table by calling the same method, mysql_query(). Before we delve any further, note that MySQL runs with autocommit mode enabled by default. This means that as soon as you execute a statement that updates (modifies) a table, MySQL stores the update on disk to make it permanent. To disable autocommit mode, use the following statement in your MySQL environment:
After disabling autocommit mode by setting the autocommit variable to zero, changes to transaction-safe tables such as those for InnoDB and NDBCLUSTER are not made permanent immediately. You must explicitly use COMMIT to store your changes to disk or ROLLBACK to ignore the changes.
To disable autocommit mode for a single series of statements, use the START TRANSACTION statement:
- START TRANSACTION;
- INSERT INTO <table> VALUES (..);
- COMMIT;
复制代码
With START TRANSACTION, autocommit remains disabled until you end the transaction with COMMIT or ROLLBACK. The autocommit mode then reverts to its previous state.
The following code fragment tries to insert two rows, but only one of those records is committed to the database, whereas the other one is discarded.
- ...
- ...
- $qry1 = "INSERT INTO City VALUES ('London, UK')";
- $qry2 = "INSERT INTO City VALUES ('Paris, France')";
- mysql_query ("START TRANSACTION", $cxn)
- or die ("Could not execute the START TRANSACTION." . mysql_error());
- mysql_query ($qry1, $cxn)
- or die ("Could not execute the INSERT." . mysql_error());
- mysql_query ("COMMIT", $cxn)
- or die ("Could not COMMIT." . mysql_error());
- mysql_query ("START TRANSACTION", $cxn)
- or die ("Could not execute the START TRANSACTION." . mysql_error());
- mysql_query ($qry2, $cxn)
- or die ("Could not execute the INSERT." . mysql_error());
- mysql_query ("ROLLBACK", $cxn)
- or die ("Could not COMMIT." . mysql_error());
- ...
- ...
复制代码
|
|