PHP PDO How to use _php tutorial

Source: Internet
Author: User
This article to give you a detailed introduction of the PHP PDO how to use the details of the students are welcome to enter the reference, there is a need to know friends can collect this article.


Pdo::exec

Returns an int type that represents the number of bars that affect the result.

Pdostatement::execute

Returns a Boolean that indicates a successful execution and false indicates that 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 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

Extension=pdo.so
Extension=pdo_mysql.so

And then in the program, we need to announce 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 setting in this article is my personal habit, you don't have to be like me?

When the operation is like above, the $DBH itself represents the PDO connection.

What do you do with PDO?

First type lazy man method query

Don't even think about it. Use the query function as usual

Code: [SELECT]

The code is as follows Copy Code

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


The second type automatic bring into method prepare

After I personally use PDO, I prefer to use the Prepare function to move

The advantage of prepare is that you can write SQL code first, and then automatically bring in the information we want later.
I think the biggest benefit is that it can reduce a lot of security problems than using query directly.

First, we use the prepare to set the SQL code, in the use of 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 ();


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

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 not do that? Complex, like: SN, although also used Bindparam to specify, but we omit the type and length, PHP will use the variable preset form to apply.

Finally, it is the use of $sth->execute (); To perform the action.

It is basically not difficult, even can say 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

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 the possible SQL in one file, then the whole SQL part becomes the full-use variable!
Anyway, the information can be used in a ready-made way to apply it!

Well, if you use prepare's way to select, the keyword can of course be used in the same way as in 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"];
}


What's new is fetch, which is about the meaning of mysql_fetch_row ().
But in fetch () we found 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 data from a fetch, this time
The $meta element name is the field name of the repository, 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 () will also follow the smooth
The element name in addition to the field to give a number-based 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 more than two samples.

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 world-simple, whenever we use execute () execution, if there is an error
There will be content in 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 are errors! ";
Print_r ($sth->errorinfo ());
}


and $sth->errorinfo () is 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

http://www.bkjia.com/PHPjc/630674.html www.bkjia.com true http://www.bkjia.com/PHPjc/630674.html techarticle This article to give you a detailed introduction of the PHP PDO how to use the details of the students are welcome to enter the reference, there is a need to know friends can collect this article. Pdo::exec returns an int type, ...

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