"Elaborate PHP" sample chapter fourth edition Chapter 18 database abstraction layer PDO 1

Now, if you're proficient at using MySQL client software to manipulate data in the database, you can begin to learn how to use PHP to display and modify data in the database. PHP provides functions to manipulate database standard. You can use two sets of MySQL and MySQLi extension functions in PHP in version 5 or more, MySQLi is new in PHP 5, MySQL extension is improved. However, due to historical issues, a lot of old projects in PHP 4 when using the MySQL extension development, if the second development in the original project, or find some examples of learning requires developers use MySQL extension functions. If the project is a new design, it is recommended to use PDO MySQLi extension or technology described in this chapter. In addition, PHP7 fully removed the MySQL extension functions support.

 

18.1 PHP to access MySQL database server process

MySQL uses a "client / server" architecture. In the previous chapters we have been using the command line to remotely manage MySQL database server, this approach is only suitable for DBA (database administrator) or application developers and other technical personnel to manage the database. Can you make an ordinary non-technical users to manage database? The answer is yes, you can use PHP script to handle the data in the database, the MySQL PHP act as a role of "clients" of. Because the combined foreground of some graphical interface technology development through PHP program, you can easily manage the database, comparing the two architectures are shown in 18-1 is "client / server" figure.

a0d0119a913143c3a1598864bab2110a.png

Comparative Figure 18-1 "client / server" architecture of the two systems

 

使用PHP和直接使用客户端软件访问MySQL数据库服务器,原理及操作步骤是相同的,如图18-1所示,都需要向MySQL管理系统发送SQL命令,而SQL命令的执行则由MySQL系统本身去处理,再将查询处理结果返给请求的用户。在PHP,中可以将SQL语句划分为两种情况去操作:一种是有返回结果集的,像“SELECT”及“DESC表名”等语句,执行完成后还要在PHP中处理查询结果;另一种则是在执行后没有结果集的,像DML(INSERT/ UPDATE/DELETE)、DDL(CREATE/DROP/ALTER)或“SET NAMES utf8”等语句。DML语句执行成功后会对数据表记录行有影响,是我们操作的重点;DDL语句则很少在PHP中使用。PHP访问MySQL数据库的流程如图18-2所示。

35a0fccfa23848e1808961d39bea5ed8.png

图18-2  PHP访问MySQL数据库的流程

 

从图18-2中可以看出,必须让PHP程序先连上MySQL数据库服务器,再选择一个数据库作为默认操作的数据库,才能向MySQL数据库管理系统发送SQL语句。如果发送的是INSERT、UPDATE或DELETE等SQL语句,MySQL执行完成并对数据表的记录有所影响,则说明执行成功。如果发送的是SELECT这样的SQL语句,则会返回结果集,还需要对结果集进行处理。处理结果集又包括获取字段信息和获取记录数据两种操作,而多数情况下只需要获取记录数据即可。脚本执行结束后还需要关闭本次连接。

PHP to access MySQL database server if you are still using a MySQL PHP5, PHP7 One of the changes is to remove the MySQL extension, MySQLi recommended or pdo_MySQL, in fact, from PHP5.5 start, begin to prepare abandoned PHP MySQL extension, extension, we may see such a prompt "deprecated: mySQL_connect (): the MySQL extension is deprecated and will be removed in the future: use MySQLi or PDO instead in". Thus, after the procedure in order to maintain compatibility, to minimize the MySQL database connection for expansion. PDO (PHP Data Object) the emergence of PHP has reached a new height. PHP PDO extension library to access the database is defined as a lightweight, consistent interface, which provides a data access abstraction layer, so that, no matter what database you use, you can execute the query and retrieve data through the same function, which greatly It simplifies the operation of the database, and can mask the differences between the different databases. Use PDO can be easily transplanted between cross-database development programs, as well as different databases, it is the main development direction in the future PHP database processing.

 

 

Guess you like

Origin www.cnblogs.com/itxdl/p/11375158.html