Open connection to database :
An example of Select query and show error message if fail :
If you wrote $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES,false); on database open connection previously, then you don't need to write implode(" : ", $q->errorInfo())); anymore.
An example of Select query with WHERE Clause :
An example of Select query with two inputs in WHERE Clause :
The question marks on codes above are called as "Position Placeholder".
Below is an example of using "Named Placeholder". The placeholder is not depends on position of value, it depends on name. That's why it called "Named Placeholders" :
Count Result (must use count(*) and $q->fetchColumn()) :
Check if sql query successful or fail :
First example of Insert query with PDO Prepared :
Second example of Insert query with PDO Prepared :
An example of Update query with PDO Prepared :
Persistent connections are known as way to improve performance. When a persistent connection is requested, PHP checks if there's already an identical persistent connection from earlier remained open. If it exists, it uses it. What meaning of remained open? Persistent connection are links that do not close when the execution of your script ends. An 'identical' connection is a connection that was opened to the same host, with the same username and the same password. We can use PDO::ATTR_PERSISTENT to do persistent connection :
Check connection attribute. Get value from connection attributes. We can use getAttribute(). Look this example :
Output will be :
mysql
5.0.51a
5.1.32-community-log
More tutorial at http://www.phpeveryday.com/articles/PDO-Error-Mode-Attributes-P558.html.
The different between two prepared statements :
1) fake / emulate pdo prepared statement (client-side prepared statement).
2) real / native pdo prepared statement (server-side prepared statement).
This SQL statement is wrong - "SELECT ice FROM artica AND antarctica" because spelling mistake of "artica", the correct spelling is "arctic".
fake / emulate pdo prepared statement will treat the sentence is right because its database doesn't require to check the statement.
real / native pdo prepared statement will check it and will know the sentence has error, and will treat the sentence is wrong.
For insert query, the real / native pdo / Server prepared statement is slower 10% than fake / emulate pdo / Client ps.
Client PS = 13.5645 seconds.
Server PS = 15.5019 seconds.
But if you use bindParam(), Server PS is 10% faster than Client PS. Eg,
For select query, both client ps and server ps is the same speed but server ps become more faster if select more LARGE or MANY records.
The default setting use fake / emulate prepared statement. If you want to use real / native pdo prepared statement, u can use the code : setAttribute(PDO::ATTR_EMULATE_PREPARES,false);More info at : http://blog.ulf-wendel.de/?p=187#pdo.
|
---|
Tuesday, October 25, 2011
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment