pdo::exec
Returns the int type representing the number of bars that affect the result.
Pdostatement::execute
Returns a Boolean, True indicates successful execution, false indicates execution failed.
These two usually come out now:
The code is as follows |
Copy Code |
$rs 0 = $pdo->exec ($sql); $pre = $pdo->prepare ($sql); $rs 1 = $pre->execute (); |
In general, you can use the value of $RS0 to determine whether the success of SQL execution, if its value is false for SQL execution failure, 0 means that there is no change, the value greater than 0 indicates how many records are affected.
However, $RS1 can only return SQL execution success or not, and use $pre->rowcount () if you need to obtain the number of records affected;
I personally like to use MySQL, so I have these two lines in the Extensions.ini
Extension=pdo.so
Extension=pdo_mysql.so
And then in the program, we need to declare PDO ⒍ tomb δ?/p>
Code: [SELECT]
The code is as follows |
Copy Code |
Define (' db_name ', ' test '); Define (' Db_user ', ' test '); Define (' db_passwd ', ' test '); Define (' db_host ', ' localhost '); Define (' Db_type ', ' MySQL '); $DBH = new PDO (db_type. ': host= '. Db_host. '; Dbname= '. Db_name, Db_user, DB_PASSWD); |
The use of constant settings in this article is my personal habit, you don't have to be like me? Trouble
When the operation like the above, $DBH itself represents the PDO connection.
How about using PDO?
The first type of lazy people law query
Don't even think about it, like usual. Using the functions of query
Code: [SELECT]
The code is as follows |
Copy Code |
$sql = ' SELECT * from Test '; foreach ($dbh->query ($sql) as $value) { echo $value [col]; }; |
Prepare of the second type automatic bringing into law
I personally use the PDO, I prefer to use the prepare function to do the action
The advantage of prepare is that you can write SQL code first, and then automatically bring in the information we need later.
I think the biggest benefit is that you can reduce a lot of security problems by using query directly
First, we use the prepare to set the SQL code, and we use the Bindparm to set the action
Code: [SELECT]
The code is as follows |
Copy Code |
$sth = $DBH->prepare (' Update db set zh_cn=: str where sn=:sn '); $sth->bindparam (': Str ', $STR, PDO::P aram_str,12); $sth->bindparam (': SN ', $SN); $sth->execute (); |
Note the following: STR and: SN, when we take advantage of the Bindparam function, we can use: Word to specify the part that the system needs to be applied, such as we use: STR and: SN to specify
and the actual content, depends on Bindparam also can specify we want to enter the form.
First we look at: STR's designation: STR because I'm sure the data belongs to the text, so using PD::P aram_str to tell the program "This is a string yo" and give a range, that is, the length is 12 bits.
Can we not have that? complex, like: SN, although it is also specified with Bindparam, but we omit the form and length, PHP will use the variable preset type.
Finally, the use of $sth->execute (); To perform the action.
Basically not difficult, even can say very simple!
If you have a large amount of data that needs to be duplicated, you can use Bindparam to specify, like my: Str and: SN If there are 10 data, I can add it directly to the database
Code: [SELECT]
The code is as follows |
Copy Code |
$sth = $dbh->prepare (' INSERT INTO db ("ZH_CN", "ZH_TW") VALUES (: str,: SN '); foreach ($array => $value) { $sth->bindparam (': Str ', $value [str],pdo::P aram_str,12); $sth->bindparam (': SN ', $value [SN]); $sth->execute (); } |
Even the strong like my friend ... Write all of the possible SQL in one file, and then the part of the process SQL becomes a full variable.
Anyway, the information can be applied in ready-made way!
So, if you use prepare to select, the keyword can of course be used like the above: Word to specify
Code: [SELECT]
The code is as follows |
Copy Code |
$sth = $dbh->prepare (' select * from db where sn =: sn '); $sth->bindparam (': SN ', $value [SN]); $sth->execute (); while ($meta = $sth->fetch (PDO::FETCH_ASSOC)) { echo $meta ["name"]; } |
This is the new fetch, which is the same as mysql_fetch_row ().
But in the fetch () we found one more pdo::fetch_assoc.
Fetch () provides many ways to get information, and PDO::FETCH_ASSOC refers to the name and value of the field that returns the next piece of information.
For example, the use of $meta to obtain the data returned by the fetch, at this time
The $meta element name is the field name of the database, and the content is, of course, the value itself
This is not the same as when you use Mysql_fetch_row (), because in addition to the field name, Mysql_fetch_row () will be in accordance with the smooth
The element name is given an element that is based on an ordinal number in addition to the field, isn't it PDO?
Of course, as long as the PDO::FETCH_ASSOC changed to Pdo::fetch_both, that usage with mysql_fetch_row () no two kind of.
How to remove errors
Error is all programmers in the heart forever pain, we use PDO to how to debug?
In fact, PDO has provided two very convenient functions
ErrorInfo () and ErrorCode ()
Usage is also world simple, whenever we use execute () after execution, if there is an error
There will be content in the ErrorInfo () and ErrorCode ().
We can do it this way ....
Code: [SELECT]
The code is as follows |
Copy Code |
$sth = $dbh->prepare (' select * from db where sn =: sn '); $sth->bindparam (': SN ', $value [SN]); $sth->execute (); if ($sth->errorcode ()) { echo "There is a mistake! There's a mistake! "; Print_r ($sth->errorinfo ()); } |
and $sth->errorinfo () is an array with three values
0 for SQLSTATE error code
1 The error code returned by the Driver you are using
2 error messages for the Driver you are using