Affinity PDO, affinity Network

Source: Internet
Author: User

Affinity PDO, affinity Network

Cause

I have never paid attention to database-related knowledge.

A few months ago, I did not intend to open the following code:

It was written N years ago. Later, it was also necessary for learning. It was no longer appropriate for a single mysql. So I searched the internet for a good method, and PDO came on the way.

Temptation

When browsing the Internet, I saw a passage:

The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP. each database driver that implements the PDO interface can expose database-specific features as regular extension functions. note that you cannot perform any database functions using the PDO extension by itself; you must use a database-specific PDO driver to access a database server.

PDO provides a data-access authentication action layer, which means that, regardless of which database you're using, you use the same functions to issue queries and fetch data. PDO does not provide a database failed action; it doesn't rewrite SQL or emulate missing features. you shoshould use a full-blown construct action layer if you need that facility.

Translation of netizens:

The PDO extension defines a lightweight and persistent interface for PHP to access the database. Each database driver that implements the PDO interface can express their respective characteristics in the form of regular extension. Note: Using PDO Extensions cannot implement any database functions. You must use a specific database PDO driver to access the database.
PDO provides a data access abstraction layer, which means that no matter which database you are using, you can use the same function for data query. PDO does not provide data extraction. It does not rewrite SQL statements or mimic these functions. You need to use a mature extraction layer if you need it.

It is not limited by the specific syntax of the database, making it easier to switch the database platform.


First Sight

To do well, you must first sharpen your tools. The installation of php5 is exhausted. The following describes the configuration of php. ini of pdo.

First, you need to know:

In php. ini, the semicolon represents a comment. Removing the semicolon above indicates that this function can be used.

OK. Open php. ini now (you can open it in Notepad)

Find the following lines in the file:

Extension = php_pdo.dll

Extension = php_pdo_firebird.dll

Extension = php_pdo_informix.dll

Extension = php_pdo_mssql.dll

Extension = php_pdo_mysql.dll

Extension = php_pdo_oci.dll

Extension = php_pdo_oci8.dll

Extension = php_pdo_odbc.dll

Extension = php_pdo_pgsql.dll

Extension = php_pdo_sqlite.dll

Remove the semicolon (I have removed it here.

Be careful when configuring php. ini. Back up before modification. I made a mistake once, making php unable to work, and I still don't know where the problem is ..

In this way, the pdo installation is complete.

Playing

The first thing is to see how to connect to the database.

It has a PDO class, which is used to create a new pdo object.

Look at the syntax. The first statement is coming out ~ \ (Too many rows )/~

In this example, mysql is the database type, localost is the host name, test is the database name, dbuser is the user name, And dbpass is the password.

Note that you should always encapsulate your PDO operations in a try/catch code block and use the exception mechanism.

The following is a statement diagram of a netizen, which combines my set into a sentence.

 

Below is error handling

PDO provides three different error handling policies.

PDO: ERRMODE_SILENT

This is the default mode. PDO sets simple error codes on statement and database objects. You can use the PDO-> errorCode () and PDO-> errorInfo () methods to check errors; if the error is caused by calling the statement object, you can use the PDOStatement-> errorCode () or PDOStatement-> errorInfo () method on the object to get the error information. If the error is caused by calling the database object, you should call the two methods on the database object.

PDO: ERRMODE_WARNING

As an additional error code, PDO sends out a traditional E_WARNING message. This setting is useful in debugging and debugging. If you just want to see what went wrong and don't want to interrupt the program process.

PDO: ERRMODE_EXCEPTION

As an attachment for setting the error code, PDO throws a PDOException and sets its attributes to reflect the error code and error information. This setting is also useful in addition to errors, because it will effectively point to an error point in the "enlarge (blow up)" script, very quickly pointing to a possible error area in your code. (Remember: if an exception causes a script interruption, the transaction will be automatically rolled back .)

Exception mode is also very useful, because you can use a structure that is clearer than the traditional PHP-style error handling structure, it uses less code and nesting than quiet mode, and it can also check the return values of each database access more clearly.

Return to the above Code. If an error occurs, a PDOException exception object will be thrown, catch will be caught, and then proceed according to the content set by the developer.

Connect to it ..

Attack

What should I do after the connection? That's right, processing data, or using databases. The following two versions are provided:

 

Assume that there is a table zjyz.

Id

Name

1

Aaa

2

Bbb

 

Data Query

We want to query data with id 1

MYSQL version:

 

PDO version:

 

Ah, it seems that there is not much difference ~~

 

Data Update

We want to update the data with id 1 and change the name value to ccc.

MYSQL version:

 

PDO version:

It seems that exec is much refreshing.

 

Delete data

We want to delete data with id 1

MYSQL version:

 

PDO version:

Haha, the number of lines returned by exec

 

Insert data

We want to insert data id = 3, name = ccc

MYSQL version:

 

PDO version:

 

Fun exec ()

This function has been shown many times. Now I will briefly introduce it.

PDO: exec () executes an SQL statement in a separate function call and returns the number of rows affected by this statement.

PDO: exec () does not return results from a SELECT statement. For SELECT statements that only need to be issued once in a program, you can consider using PDO: query (). For statements that need to be issued multiple times, use PDO: prepare () to prepare a PDOStatement object and use PDOStatement: execute () to issue the statement.

 

Summary

Although this technology is a little old, it is still vigorous compared with the old statements. As I learned more deeply, I learned a lot of things that I didn't mention in this article. Thank you ~ \ (Too many rows )/~

And so on... All operations involving databases are also basically replaced with PDO

Glad to hear that

Technology is dead, and people are active. Use old technologies to create a new future.

References:

 

Copyright

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.