5.1MySQL of available APIs
To facilitate the development of applications, MySQL provides a client library in the C programming language that allows access to the MySQL database from within any C program. The client library implements the application programming interface (A P I), which defines how the client program establishes and executes communication with the server.
However, using C to write MySQL programs is not limited. Many other language processors are also written by C, or have the ability to use C libraries, so the MySQL client library provides this method, and MySQL's constraints on these languages can be built on the C API. This provides a number of options for writing applications that communicate with MySQL servers. The client program's APIs are written in Perl, PHP, Java, Python, C + +, TCL, and other languages. For the latest listings, check the MySQL reference guide or the Mysqlweb site, because it sometimes adds a P I written in the new language.
Each language constraint defines its own interface, especially the rules for accessing MySQL. There is not enough time to discuss each of the a P I that MySQL can use, we only talk about three of the most popular:
C Client library a P I. This is the basic programming interface for MySQL.
Perl Universal Target scripting language DBI (Database interface) a P I. DBI is implemented as a Perl module with other modules in DB D (database driver)-level interfaces, each of which provides access to a specific type of database engine (and, of course, the specific DBD modules we will discuss also provide support for MySQL). The most common use of DBI for MySQL is to write stand-alone clients that are invoked by the command line, and scripts that attempt to be invoked by the Web server to provide Web access to MySQL.
PHP API. PHP is a scripting language that provides a convenient way to embed programs in a Web page. Before sending, such a page is handled by PHP, which allows these scripts to generate dynamic content, such as the results of MySQL queries on the page. The original meaning of "PHP" is the personal homepage (Personal home page), but PHP has grown far beyond its simple original functionality. The name that the PHP Web site now uses is the "PHP: Hypertext Preprocessor (hypertext Preprocessor)", which is referenced in the same way as GNU (gun rather than Unix).
Use the results of others
When a standard MySQL client doesn't meet your needs, you don't always need to write your own program. Others have been writing programs that many of these programs can share. Please refer to some examples in appendix I. You can save a lot of work by just finding a few.
The above three kinds of a P I have a specific section detailed description. This chapter provides an overview of API comparisons to illustrate their basic characteristics, and gives reasons why a particular application might choose one rather than another.
Of course, you don't have to think about just one API, you should know each API, and you can choose your own APIs wisely. In large projects that include several components, you may use multiple APIs, multiple languages, depending on which language each subtask fits.
For any API you are trying to use, if you need to get the necessary software, see Appendix A.
5.1.1C API
CAPI is used to compile the inside of a C program context environment. It is a customer library that provides the lowest level of interface that can be used to talk to MySQL servers-with the ability to create communication with the server. The predecessor of DBI and PHP, DBI Perl, was the Mysqlperl module M y s q L. P M. This module is no longer supported and should not be used for new MySQL development. One thing to understand is that Mysqlperl is dependent on MySQL, but DBI is not. If you write MySQL's Perl applications and then decide that you want to use them with a different database engine, porting DBI scripts is easier than Mysqlperl scripts because they are rarely dependent on a particular database engine.
If you get a Perl script that accesses MySQL and find that it was written in Mysqlperl instead of DBI, you can still use DBI. DBI includes emulation support for Mysqlperl, so there is no need to install two packages. The predecessor of PHP 3 is PHP/FI 2.0 (F I represents "form interpreter, or Format interpreter"). Like Mysqlperl, php/f i is obsolete, so we don't discuss it any further.
The origins of the MYSQLC API if you already have experience writing a msql RDBMS program, you'll notice that the MYSQLC API is similar to msql the corresponding C API. Many useful shared utilities are available for M SQL when MySQL developers start implementing their SQL engine. To minimize the difficulty of porting those msql utilities to MySQL
The utility is possible to intentionally design the MYSQLAPI as a msql2mysql script similar to the mSQL API (MySQL even has a simple text substitute with the MySQL name corresponding to the mSQL API function name). This operation is relatively cumbersome and actually takes care of many of the work involved in converting MSQL programs for use with MySQL.
The C client provided by the MySQL distribution contract is based on this API. The C client library also serves as a basis for MySQL for other language constraints, but the Java API is an exception. For example, by connecting to the MYSQLC client library code (which is illustrated in Appendix A with the DBI and PHP installation instructions), MySQL can be used in the Perl DBI module proprietary MySQL driver and PHP code.
5.1.2 Perl DBI apidbi APIs are used within the context of an application written in the Perl scripting language. This API is the highest of the three API architectures we consider because it works with many databases, while the details of many specific databases can be ignored in scripts. DBI is implemented through a Perl module with a level two structure (see Figure 5-1):
DBI (Database Interface) level. Provides an interface for client script. This level provides an abstract interface and does not refer to a particular database engine.
DBD (Database Drive) level. At this level, the driver for a particular engine provides support for the various database engines.
Mysql's support environment for DBI is provided by the Msql-mysql-modules distribution contract. This module operates at the DBD level. It can be distinguished from the distribution name and figure 5-1, and a driver can provide support for more than one RDBMS. Msql-mysql-modules was originally written for mSQL and later extended to Mysql. This effect is similar to the CAPI of mSQL and MySQL. Since the design of the MYSQLCAPI is similar to the mSQL CAPI, it makes sense to extend mSQL DBD (using the mSQL C API) to the use of MySQL.
The DBI architecture writes applications in a relatively common style. When writing DBI scripts, you can use a standard set of calls. The DBI level invokes the appropriate driver at the DBD level to handle the request, which is handled by the driver for specific problems that are included in the specific database server communication that you want to use. The DBD level transmits data returned from the server, backed up to the DBI level, enabling
The data appears in the application. The format of the data is consistent with the data source of the database.
The result is an interface that hides the differences between database engines from the perspective of the creator of the application, so that there are many different engines--as many as drivers. DBI provides a consistent customer interface to increase portability by allowing a unified style of access to each database.
When you open a database, a database-specific interface written by the script appears. When you create a connection, you should indicate which driver to use. For example, to use the MySQL database, you should connect this way:
To use Postgres or M SQL, you should connect this way:
After you connect, you do not need to make any special references to the driver. Let DBI and the driver solve the database proprietary details.
In any case, this is a theoretical problem. However, at least two factors are incompatible with the portability of the DBI script:
Unlike the implementation of SQL between RDBMS engines, it is entirely possible for another engine to be written for one engine that is not understood at all. If the SQL is fairly generic, the script can migrate between the engines accordingly. But if SQL relies on the engine, so is the script. For example, if you use the show tables statement specified by MySQL, the script cannot be executed with a different database.
The DBD module usually provides the engine-specific type of information to allow script writers to use particular features of a particular database system. For example, MYSQLDBD provides methods to access column properties in query results, such as the maximum length of each column, whether the column is numeric, and so on. These attributes do not have any similarity in other databases. The proprietary characteristics and portability of DBD, by using them, scripting MySQL scripts for other database systems is difficult (however, in the 7th chapter, you will find that the author effortlessly avoids the MySQL-specific architecture provided by MYSQLDBD). That's because you should know what the structure is so that you can decide if you want to use them.
Although there are two factors for database proprietary scripting, the DBI mechanism for providing database access in an abstract way is a reasonable way to accomplish portability, as long as you decide how many times to use it.
5.1.3 PHP API
Like Perl, PHP is also a scripting language. But unlike Perl, PHP is rarely designed as a common target language, but as a language for writing Web applications. The PHP API is used primarily as a way to embed executable scripts in Web pages. This makes it easy for Web developers to write pages using dynamically generated context environments.
When a client browser sends a request for a PHP page to a Web server, PHP executes any script it finds on the page and replaces it with the output of the script. The results are then sent back to the browser. This makes the pages that actually appear in the browser different depending on the requested page environment. For example, when the following short PHP script is embedded in a Web page, it appears with the host I p address of the requested page:
You can use scripts to provide visitors with the latest information based on the context of the database. The following sample description can be used for historical League Web Mirror Yie capsule Huan Strider, ソ malaria 8 vector malaria spasm ⒉ home Huan Luan 肭 the number of Ǖ of the nasal 腖, and report the number to the person who visited the site (the script does not report any number if an error occurs):
The meaning of DBI and dbd
Although the DBI level is independent of the database, and the DBD level is dependent on the database, but that is not "DBI" and "DBD" represents the meaning. They mean "Database interface" and "Database driver."
PHP scripts usually look like they are embedded with " P H P "and"? > The HTML page of the script in the identifier. A page may include several scripts. This provides a very flexible way to develop a script. For example, if you like, you can write a normal HTML page to create a generic page frame, and then add the script to the inside
Capacity.
For different database engines, PHP does not do anything with the unified interface, DBI also use this method. Instead, the interface of each engine looks much like the corresponding C library interface that implements the engine's low-level API. For example, the name of a PHP function used to access MySQL from within a PHP script is very similar to the name of a function in the MYSQLC client library.