Introduction
Php5.1. * And php5.2. * are found to be different during Database Pre-compiled code 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
The database connection code is the same.
Copy codeThe Code is as follows:
$ 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)
Copy codeThe Code is as follows:
$ 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. *
Copy codeThe Code is as follows:
$ Stmt = $ dbh-> prepare ('select * from t1 where name =: name limit 2 ');
$ Params = array ();
$ Params [] = 'renta'; // ":" can be successfully executed without adding ":".
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 ();
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)
Copy codeThe Code is as follows: $ stmt = $ dbh-> prepare ('select * from t2 where name =: name limit: page ');
$ Params = array ();
$ Params ['name'] = 'renta'; // ":" can be successfully executed without adding ":".
$ Params ['page'] = 2;
Foreach ($ params as $ k => $ 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
Copy codeThe Code is as follows:
$ 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 |
Summary
When using PDO for Database Pre-compilation, PHP tries its best 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.
Copy codeThe Code is as follows: scp-P9888 index. php rentao@192.168.10.4:/home/rentao