PHP using PDO method, Phppdo detailed _php tutorial

Source: Internet
Author: User
Tags php programming

PHP using PDO method, Phppdo detailed


This article analyzes the use of the PDO method in PHP. Share to everyone for your reference. The specific analysis is as follows:

Pdo::exec: Returns the int type that represents the number of bars that affect the result.
Copy the Code code as follows: Pdostatement::execute

Returns a Boolean that indicates a successful execution and false indicates an execution failure, both of which are typically shown in the following code:
Copy the 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 SQL execution succeeds, or if its value is false to indicate that SQL execution failed, 0 means that there are no changes, and a value greater than 0 indicates how many records are affected.

However, $RS1 can only return SQL execution success or not, if the number of records required to obtain an impact requires the use of $pre->rowcount ();

I personally like to use MySQL, so I have these two lines in my Extensions.ini.
Copy the Code code as follows: extension=pdo.so
Extension=pdo_mysql.so

Then in the program, the code is as follows:
Copy the 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 constant settings in the text is my personal habit, you do not like me so troublesome, when like the above operation, $DBH itself is to represent the connection of PDO, then how to use PDO?

The first kind, lazy people method query, do not want to think, as usual, using the query function, the code is as follows:
Copy the code as follows: $sql = ' select * from Test ';
foreach ($dbh->query ($sql) as $value)
{
echo $value [col];
};
The second, automatically bring into the law prepare, I personally use PDO, the preference to use the Prepare function to do the prepare advantage is that you can write the SQL code, and then automatically bring in the information we want later.

This I think the biggest advantage is compared with the direct use of query can reduce a lot of security problems, first, we use prepare to set the SQL code, in the use of Bindparm to set the action, the code is as follows:
Copy the 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 ();
Note the following: STR and: SN, when we use the Bindparam function, we can use: Word to specify the part that the system needs to apply, such as we use: STR and: SN to specify, and the actual content, by Bindparam can also specify the type we want to enter.

First we look at: STR designation,: STR because I am sure the data is text, so use 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 used Bindparam to specify, but we omit the type and length, PHP will use the variable preset mode to apply.

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

If you have a large amount of data that needs to be repeated, you can use the Bindparam to specify, for example, my: str and: SN If there are 10 of data, I can also directly add to the database, the code is as follows:
Copy the 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, such as my friend, all the possible SQL is written in a file, and then the process of SQL into the full use of variables, anyway, data can be applied in a ready-made way.

Well, if you use prepare's way to select, the keyword can of course be used as above: Word to specify, the code is as follows:
Copy the 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"];
}
This new fetch is similar to that of Mysql_fetch_row (), but in fetch () we find that there is one more pdo::fetch_assoc this thing.

Fetch () provides a number of ways to get information, and PDO::FETCH_ASSOC refers to the field name and value of the next information returned.

For example, using $meta to obtain the data of the fetch, at this time $meta element name is the database field name, and the content is of course the value itself this is not the same as you use Mysql_fetch_row (), because in addition to the field name, Mysql_fetch_ Row () Also in accordance with the smooth element name in addition to the field to give a number of elements based on the element, that is not PDO?

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

How to remove the wrong

In addition to the mistakes of all programmers in the hearts of the eternal pain, we use PDO how to remove the wrong?

In fact, PDO has provided two very convenient functions errorinfo () and ErrorCode ()

Usage is also the world simple, whenever we use execute (), if there is an error, then ErrorInfo () and ErrorCode () will have content, we can do this, the code is as follows:
Copy the 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 are errors! ";
Print_r ($sth->errorinfo ());
}
The $sth->errorinfo () will be an array with three values:

0 for SQLSTATE error code

1 error codes returned for the Driver you are using

2 error messages returned for the Driver you are using

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/933594.html www.bkjia.com true http://www.bkjia.com/PHPjc/933594.html techarticle PHP uses the PDO method in detail, phppdo detailed analysis of PHP using the PDO method. Share to everyone for your reference. The specific analysis is as follows: Pdo::exec: The type of int is returned, indicating the effect of the knot ...

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