This section describes how to pre-compile php5.1. * And php5.2. * in the database. Code There are differences during execution. Advantages of pre-Compilation
1. Use placeholders to avoid entering data in SQL words. Automatically handle escape of characters such as quotation marks and backslash-increase security.
2. "prepare" a statement in advance and bind different values for reuse each execution. -- It is often used for statements that are executed multiple times later.
3. High readability.
Code database connection code is the same.
$ Protol = 'mysql: host = localhost; dbname = test'; $ username = 'monty '; $ passwd = '000000'; $ DBH = new PDO ($ protol, $ username, $ passwd );
Below are some tests.Note the SQL and for or foreach statements!
Test 1 (bind with key value)
$ Stmt = $ DBH-> prepare ('select * from T1 where name =: name'); $ Params = array (); $ Params ['name'] = 'renta'; foreach ($ Params as $ K =>$ v) {$ stmt-> bindparam ($ K, $ V );} $ stmt-> execute (); $ item = array (); While ($ ROW = $ stmt-> fetch (PDO: fetch_assoc) {var_dump ($ row );} $ stmt = NULL; $ DBH = NULL;
Conclusion:
PHP 5.1 .* |
PHP 5.2 .* |
Normal execution |
Normal execution |
$ Params ['name'] = 'renta' and $ Params [': name'] = 'renta' can be executed, meaning they are not limited. |
Test 2 (bind the number subscript, but the starting parameter of the binding is 1) -- ": Key" cannot be bound with the number subscript on php5.2. *
$ Stmt = $ DBH-> prepare ('select * from T1 where name =: Name limit 2'); $ Params = array (); $ Params [] = 'rentao '; // ":" can be successfully executed for ($ I = 0, $ ilen = count ($ Params); $ I <$ ilen; $ I ++) {$ k = $ I + 1; $ stmt-> bindparam ($ K, $ Params [$ I]);} echo "here1 \ n "; $ stmt-> execute (); echo "here2 \ n"; $ item = array ();
Introduction
While ($ ROW = $ stmt-> fetch (PDO: fetch_assoc) {var_dump ($ row) ;}$ stmt = NULL; $ DBH = NULL;
Conclusion:
PHP 5.1 .* |
PHP 5.2 .* |
Normal execution |
Error: "PHP warning: pdostatement: Execute (): sqlstate [hy093]: Invalid parameter number: parameter was not defined" |
If you change ": Name" "? ", Then both versions can proceed smoothly. |
You cannot use two symbols at the same time. For example, select * From T2 where name =?Limit: Page |
Test 3 (Limit binding: page)
$ Stmt = $ DBH-> prepare ('select * From T2 where name =: Name limit: page'); $ Params = array (); $ Params ['name'] = 'renta'; // If ":" is not added here, $ Params ['page'] = 2 can be successfully executed; foreach ($ Params as $ K =>v v) {$ stmt-> bindparam ($ K, $ v) ;}$ stmt-> execute (); echo "here1 \ n"; $ item = array (); While ($ ROW = $ stmt-> fetch (PDO: fetch_assoc) {var_dump ($ row );} echo "here2 \ n"; $ stmt = NULL; $ DBH = NULL;
Conclusion:
PHP 5.1 .* |
PHP 5.2 .* |
When running to $ stmt-> execute (), the process remains in the waiting state |
Normal execution: no result is printed |
Test 4 (perform the pre-compilation operation under limit: page) -- use "?" Mechanism operation
$ Stmt = $ DBH-> prepare ('select * From T2 where name =? Limit? '); $ Params = array (); $ Params [] = 'renta'; $ Params [] = 2; for ($ I = 0, $ ilen = count ($ Params); $ I <$ ilen; $ I ++) {$ k = $ I + 1; $ stmt-> bindparam ($ K, $ Params [$ I]) ;}$ stmt-> execute (); $ item = array (); While ($ ROW = $ stmt-> fetch (PDO :: fetch_assoc) {var_dump ($ row) ;}$ stmt = NULL; $ DBH = NULL;
PHP 5.1 .* |
PHP 5.2 .* |
Normal execution |
Normal execution: no result is printed |
Test 5 --- order)
PHP 5.1 .* |
PHP 5.2 .* |
Output results, but not sorted by order |
Normal execution: no result is printed |
For writing to a problematic database, we use function execute () {}) to bind numeric subscripts (){}). Therefore, all write operations must be performed "? ". Otherwise, binding is not supported. Summary when PHP uses PDO for Database Pre-compilation, try to avoid using limit, order by, and group by for pre-compilation. To bind variables, we should try to use unified standards, or else we should use "?", Otherwise, use ": key ". Useful commands: I tested php5.1. *. After the test, I transmitted the file to php5.2. * server through SCP.
SCP-p9888 index. php rentao@192.168.10.4:/home/rentao