Home > Backend Development > PHP Tutorial > Join PDO, Join Net_PHP Tutorial

Join PDO, Join Net_PHP Tutorial

WBOY
Release: 2016-07-13 10:12:22
Original
772 people have browsed it

Join PDO, Jiewang.com

Cause

I have never paid attention to database related knowledge

A few months ago, I accidentally opened the following piece of code:

Being complained about was a way of writing N years ago. Later, it was also necessary for learning, and mysql alone was no longer suitable. So I searched online for good methods, and PDO came to me.

Temptation

While 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 abstraction 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 abstraction; it doesn't rewrite SQL or emulate missing features. You should use a full-blown abstraction layer if you need that facility. 

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扩展为PHP定义了一个访问数据库的轻量的,持久的接口。实现了PDO接口的每一种数据库驱动都能以正则扩展的形式把他们各自的特色表现出来。注意;利用PDO扩展本身并不能实现任何数据库函数。你必须使用一个特定的数据库PDO驱动去访问数据库。
PDO提供了一个数据访问抽象层,这就意味着,不管你使用的是哪种数据库,你都可以用同样的函数去进行查询的获取数据。PDO并不提供数据提取,它不会重写SQL语句,或者模仿这些功能。你需要使用一个成熟的提取层,如果你需要的话。

PDO provides a data-access abstraction 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 abstraction; it doesn 't rewrite SQL or emulate missing features. You should use a full-blown abstraction layer if you need that facility.

gives the translation given by netizens:


The PDO extension defines a lightweight, persistent interface for PHP to access databases. Each database driver that implements the PDO interface can express their respective characteristics in the form of regular expansion. Note: Using the PDO extension itself does not 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 to query and obtain data. PDO does not provide data extraction, it does not rewrite SQL statements, or mimic these functions. You need to use a full-blown extraction layer if you need it.

It is not restricted by database-specific syntax, making database platform switching more painless. It’s very tempting, isn’t it?

First meeting If a worker wants to do his job well, he must first sharpen his tools. The installation of php5

has been described in detail. Let’s talk about the

php.ini of pdo

Configuration of First, you need to know:

In php.ini, the semicolon represents a comment. Removing the preceding semicolon means that the function can be used

Ok, now open

php.ini

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

(can be opened with notepad) In the file, find the following line
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

Just remove the semicolon in front (I have already removed it here).

Be careful when configuring php.ini and back it up before modifying it. I once corrected a mistake and made php unable to work. I still didn’t know what the problem was. .

In this way, pdo is installed

Play with it

The first thing is of course to see how to connect to the database

It has a PDO class, which says to create a new pdo object, which should be This is it

Look at the grammar, the first statement comes out~(≧▽≦)/~

In it, mysql is the database type, localost is the host name, test is the database name, dbuser is the user name, dbpass is the password

Please note that try/catch code blocks, you should always encapsulate your PDO operations Within a try/catch block and use exception mechanism

Below is a netizen’s sentence diagram, which I put together into one sentence

The following is error handling

PDO provides different error handling strategies in 3.

PDO::ERRMODE_SILENT

这是默认使用的模式。PDO会在statementdatabase对象上设定简单的错误代号,你可以使用PDO->errorCode() PDO->errorInfo() 方法检查错误;如果错误是在对statement对象进行调用时导致的,你就可以在那个对象上使用 PDOStatement->errorCode() PDOStatement->errorInfo() 方法取得错误信息。而如果错误是在对database对象调用时导致的,你就应该在这个database对象上调用那两个方法。

 

PDO::ERRMODE_WARNING

作为设置错误代号的附加,PDO将会发出一个传统的E_WARNING信息。这种设置在除错和调试时是很有用的,如果你只是想看看发生了什么问题而不想中断程序的流程的话。

PDO::ERRMODE_EXCEPTION

为设置错误代号的附件,PDO会抛出一个PDOException异常并设置它的属性来反映错误代号和错误信息。这中设置在除错时也是很有用的,因为他会有效的"放大(blow up)"脚本中的出错点,非常快速的指向一个你代码中可能出错区域。(记住:如果异常导致脚本中断,事务处理回自动回滚。)

异常模式也是非常有用的,因为你可以使用比以前那种使用传统的PHP风格的错误处理结构更清晰的结构处理错误,比使用安静模式使用更少的代码及嵌套,也能够更加明确地检查每个数据库访问的返回值。

PDO::ERRMODE_SILENT This is the default mode. PDO will set simple error codes on the statement

and

database objects , you can use the

PDO->errorCode()

and PDO->errorInfo()

methods to check for errors; if the error is Caused when calling

statement object, you can use

PDOStatement->errorCode()

or

PDOStatement->errorInfo() method to obtain error information. And if the error is caused when calling the database

object, you should call those two methods on this

id

name

1

aaa

2

bbb

database object . PDO::ERRMODE_WARNING In addition to setting the error code, PDO will emit a traditional E_WARNING message. This setting is useful when debugging and debugging if you just want to see what went wrong without interrupting the flow of the program. PDO::ERRMODE_EXCEPTION is an attachment with an error code set as , and PDO will throw a PDOException Exception and set its properties to reflect the error code and error message. This setting is also very useful when debugging, because it will effectively "enlarge (blow up)" the error point in the script, very quickly Point to an area in your code where the error may occur. (Remember: if an exception causes the script to be interrupted, the transaction will automatically roll back.) The exception pattern is also very useful because you can use a cleaner structure than the traditional PHP style error handling structure. Errors use less code and nesting than using quiet mode, and can more explicitly check the return value of each database access.
Back to the above code, if an error occurs, a PDOException exception object will be thrown, and then catch will be captured and further processed according to the content set by the developer. The connection ends here. . Attack What should I do after connecting? That's right, what is the use of a database if not to process data? All below will be given in two versions: Suppose there is a tablezjyz
id name
1 aaa
2 bbb

Data query

We want to query the data of id for 1

MYSQLVersion:

PDO Version:

Hey, it doesn’t look much different~~

Data update

We want to update the data of id to 1, name is changed to ccc

MYSQLVersion:

PDO Version:

I feel like thisexecis much more refreshing

Delete data

We want to delete the data for 1 MYSQL

Version:

PDO

Version:

Haha, still

execreturns the number of rows

Insert data

We want to insert data

id = 3 , name = ccc MYSQL

Version:

PDO

Version:

Fun

exec() This function has appeared many times above, now I will briefly introduce it

PDO::exec()

Execute a SQL statement in a single function call, and the return is affected by this statement number of rows. PDO::exec()

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

Summary

Although this technology is a bit old, it is still vibrant compared to the old sentences. As the study progressed, Loulou also learned a lot of things not mentioned in this article. Thank you everyone

~(≧▽≦)/~

These and many more. . . All operations involving the database have basically been replaced by

PDO I’m so happy

Technology is dead, but people are alive. Leverage old technology to create a new future.

References for this article:

本文参考资料:

 

版权所有

Copyright
http://www.bkjia.com/PHPjc/923412.htmlwww.bkjia.com

true
http: //www.bkjia.com/PHPjc/923412.html
TechArticleJie Yuan.com is because I haven’t paid attention to database related knowledge. A few months ago, I accidentally opened the following piece of code: Being complained about was a way of writing N years ago. Later, it was also needed for study...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template