PHP using PDO method to explain _php skills

Source: Internet
Author: User
Tags advantage

This paper analyzes the PDO method used in PHP in detail. Share to everyone for your reference. The specific analysis is as follows:

Pdo::exec: Returns the int type representing the number of bars that affect the result.

Copy Code code as follows:
Pdostatement::execute

Returns a Boolean, true indicates execution success, false indicates execution failure, and these two typically appear in the following code:

Copy Code code as follows:
$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 my Extensions.ini.

Copy Code code as follows:
Extension=pdo.so
Extension=pdo_mysql.so

Then in the program, the code is as follows:

Copy Code code as follows:
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 the constant setting is my personal habit, you do not need to be like me so troublesome, when the operation like the above, $DBH itself is to represent the PDO line, then how to use PDO?

The first, lazy query, nothing to think about, like the usual use of the function of query, the code is as follows:

Copy Code code as follows:
$sql = ' SELECT * from Test ';
foreach ($dbh->query ($sql) as $value)
{
echo $value [col];
};

the second, automatic method of bringing inPrepare, my personal use of PDO, I prefer to use the prepare function to do the prepare advantage is that you can write a good SQL code, and later automatically bring the information we want.

This I think the biggest advantage is that the direct use of query can reduce a lot of security problems, first of all, we use prepare for SQL code settings, in the use of Bindparm to do the set of actions, the code is as follows:

Copy Code code as follows:
$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 ();

Please note that the article: 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, depending on the Bindparam can also specify the type we want to enter.

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.

We can also not be so complicated, like: SN, although it is also specified with Bindparam, but we omit the form and length, PHP will use the variable preset form to apply.

Finally, the use of $sth->execute (); To carry out the action, basically is not difficult, even can say very simple.

If you have a lot of data that needs to be duplicated, you can bindparam to use it to specify, like my: Str and: SN If there are 10 data, I can add this directly to the database, the code is as follows:

Copy Code code as follows:
$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, writes all the possible SQL to a file, and the later part of the process SQL becomes full of variables, and the data can be applied in ready-made ways.

Well, if you use prepare to select, the keyword can of course be used as above: word specifies that the code is as follows:

Copy Code code as follows:
$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"];
}

So the new appearance is fetch, which is similar to the meaning of mysql_fetch_row (), but in the fetch () we find one more pdo::fetch_assoc this thing.

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, use $meta to get the data from the fetch, at which point the $meta element name is the database's field name, 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 () also according to the success of the element name in addition to the field to give a number of the way to the basis of the element, that is not PDO?

Of course, as long as the PDO::FETCH_ASSOC changed to Pdo::fetch_both, the use of mysql_fetch_row () is no different.

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

The usage is also the world simple, whenever we use execute () execution, if has the error, that ErrorInfo () and ErrorCode () will have the content, we can do this, the code is as follows:

Copy Code code as follows:
$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

I hope this article will help you with your PHP program design.

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.