Differences between PHP operations in the PDO database between 5.1.* and 5.2.* _ PHP Tutorial

Source: Internet
Author: User
Summary of differences between the PDO database operations of PHP between 5.1.* and 5.2. This section describes the differences between php5.1. * and php5.2. * during Database pre-compiled code execution. Precompilation advantages 1. use placeholders to avoid entering data into SQL words. Introduction to Automatic Processing
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.

The 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)

The 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. *

The 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)

The 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

The 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.

The code is as follows:

Scp-P9888 index. php rentao@192.168.10.4:/home/rentao

Today, php5.1. * and php5.2. * are different in database pre-compiled code execution. Precompilation advantages 1. use placeholders to avoid entering data into SQL words. Automatic Processing Guide...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.