PHP differences in PDO database operations between 5.1.* and 5.2.* _php tutorial

Source: Internet
Author: User
Introduced
Today, php5.1.* and php5.2.* are found to be different when the database precompiled code executes.
Pre-compilation Benefits
1. Use placeholders to avoid literal input of data into SQL. Automatically handles the escaping of characters such as quotes and backslashes-increasing security.
2. Pre-"prepare" a statement and then bind different values at each execution to achieve the purpose of reuse. -often used for statements that are executed more than once.
3. Readability is strong.

Code
The database connection code is the same.
Copy CodeThe code is as follows:
$protol = ' mysql:host=localhost;dbname=test ';
$username = ' Monty ';
$passwd = ' 0818 ';
$DBH = new PDO ($protol, $username, $passwd);

Here are some tests. Note the SQL and for or foreach statements inside!
Test 1 (Bind with a key value)
Copy CodeThe code is as follows:
$stmt = $dbh->prepare (' select * from t1 where Name=:name ');
$params = Array ();
$params [' name '] = ' Rentao ';
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.*
Perform normal Perform normal
$params [' name '] = ' Rentao ' and $params[': Name ']= ' Rentao ' Both can be executed, stating that there is no ":" Limit.
Test 2 (numeric subscript to bind, but binding starting parameter is 1)--": Key" cannot be bound with numeric subscript on php5.2.*

Copy CodeThe code is as follows:
$stmt = $dbh->prepare (' select * from t1 where name=:name limit 2 ');
$params = Array ();
$params [] = ' rentao ';//Add No ":" to execute successfully


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.*
Perform normal Error: "PHP warning:pdostatement::execute (): sqlstate[hy093]: Invalid parameter number:parameter was not defined"
If you change the ": Name" to "?", then the two versions will go smoothly.
Two symbols cannot be used at the same time. such as SELECT * from T2 where name=? limit :p age
Test 3 (Limit binding: page)
Copy CodeThe code is as follows: $stmt = $dbh->prepare (' select * from T2 where name=:name limit:p age ');
$params = Array ();
$params [' name '] = ' rentao ';//Add ":" to execute successfully
$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.*
Process is in a wait state when executed to $stmt->execute () Normal execution: No results are printed
Test 4 (pre-compile at limit: page)--use "? "Mechanism runs
Copy CodeThe code is as follows:
$stmt = $dbh->prepare (' select * from T2 where name=? ');
$params = Array ();
$params [] = ' Rentao ';
$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.*
Perform normal Normal execution: No results are printed
Test 5---ORDER BY)
PHP 5.1.* PHP 5.2.*
Results are printed but not ordered by order Normal execution: No results are printed
Summary
PHP when using PDO to do database pre-compilation operations, try to avoid using limit, order by, the group by do pre-compile processing. Binding variables We try to use a uniform standard, or else we use "? ", otherwise use": Key ".

Useful command, I tested it in php5.1.*, I passed the file to the php5.2.* server via SCP
Copy CodeThe code is as follows: scp-p9888 index.php Rentao@192.168.10.4:/home/rentao

http://www.bkjia.com/PHPjc/325264.html www.bkjia.com true http://www.bkjia.com/PHPjc/325264.html techarticle describes the differences found today between php5.1.* and php5.2.* when the database precompiled code executes. Pre-compilation Benefits 1. Use placeholders to avoid literal input of data into SQL. Automatic processing of primers ...

  • Related Article

    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.