PHP database driver, different ways to connect data study notes_PHP tutorial
WBOYOriginal
2016-07-13 10:31:331239browse
Table of contents
1. Introduction to PHP database driver
2. Different ways to connect to the database with PHP
1. Introduction to PHP database driver
A driver is a piece of software code designed to interact with a specific type of database server. The driver may call some libraries. Similar to the concept of database driver in Java
Copy code
1. JDBC-ODPC Bridge:
It maps JDBC API to ODPC API. Then let JDBC-ODPC call the local driver code of the database (that is, the database operation binary code library provided by the database manufacturer, such as oci.dll in Oracle)
2. Local API driver
Directly map the JDBC API to a database-specific client API, that is, load the local code library (C/C++, etc.) provided by the database manufacturer through the client
3. Network protocol driver (mainstream)
This type of driver provides a network API to the client. The JDBC driver on the client uses a socket to call the middleware program on the server, which converts its request into the required specific API calls.
4. Local protocol driver (mainstream)
This type of driver uses Socket to communicate directly between the client and the database. It is a JDBC that interacts directly with the database instance
This driver is smart. It knows the underlying protocol used by the database. It is also the most mainstream JDBC driver currently used. The focus of this chapter is on it
Copy code
For PHP, network protocol drivers and local protocol drivers are also commonly used, namely the MySQL client library and the MySQL Native driver library. These libraries implement the low-level protocols for interacting with the MySQL database server.
The database driver is at the bottom layer of communication between PHP and the database. Different database vendors will implement their own drivers based on a certain framework to provide basic functions and advanced functions of specific databases.
On top of the driver layer is the "connector" or adapter abstraction layer, which is used to connect PHP code and database. Programmers can use PDO (PHP Database Object) or directly use extended interfaces (mysql, mysqli) These exposed APIs communicate with the underlying database.
The underlying database driver provided by the database manufacturer
File is a file-based database engine and uses file I/O (input/output) functions to store and read databases from files on disk. It is generally much smaller than relational databases (such as Mysql) (such as typical file databases
The size of the SQLite command line version is less than 200KB). At the same time, the file database supports most of the SQL commands you are familiar with and is easy to carry.
Next, let’s start with the big picture above and learn one by one the different ways PHP connects to the database and their advantages and disadvantages in different business scenarios
2. Different ways of connecting to database with PHP
0x1: Use extended API interface to communicate with database
PHP code is composed of a core and some optional extensions that make up the core functionality. PHP's MySQL-related extensions, such as mysqli and mysql, are all implemented based on the PHP extension framework.
A typical function of extensions is to expose an API to PHP programmers, allowing extended functions to be used by programmers. Of course, there are also some extensions developed based on the PHP extension framework that do not expose API interfaces to PHP programmers. For example, the PDO MySQL driver extension does not expose the API interface to PHP programmers, but provides an interface to the PDO layer above it.
Please refer to another blog post about writing PHP extensions
http://www.cnblogs.com/LittleHann/p/3562259.html
In actual programming, the most frequently used method is to connect to the database by extending the API
extension=php_mysql.dll
This is an early extension designed and developed to allow PHP applications to interact with MySQL databases. The mysql extension provides a procedure-oriented interface and is designed for MySQL 4.1.3 or earlier. Therefore, although this extension can interact with MySQL 4.1.3 or newer database servers, it does not support some features provided by later MySQL servers
The source code of mysql extension is in the PHP extension directory ext/mysql
Copy code
// Connect and select database
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password') or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db('my_database') or die('Could not select database');
// 执行 SQL 查询
$query = 'SELECT * FROM my_table';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
// 以 HTML 打印查询结果
echo "
n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
/* Destroy the result set and free the memory used for it End the query to release the memory */
mysqli_free_result($result);
}
/* Close the connection Close the connection*/
mysqli_close($link);
?>
Copy code
PHP also supports many other database connection extensions. The usage methods are similar, as long as you follow the function calling specifications. For more details, please refer to
http://www.php.net/manual/zh/refs.database.php
0x2: Use PDO abstraction layer to communicate with database
PDO (PHP Data Object PHP Database Object) is a database abstraction layer specification in PHP applications. PDO provides a unified API interface that allows your PHP application to not care about the specific type of database server system to be connected. In other words, if you use PDO's API, you can seamlessly switch database servers whenever needed, such as from Firebird to MySQL, with only a small amount of PHP code modifications.
Other examples of database abstraction layers include JDBC in Java applications and DBI in Perl.
Note: Using PDO extension itself cannot implement any database functions; a PDO driver of a specific database must be used to access database services (it is just an interface specification)
But on the other hand, the stronger the compatibility an interface provides, the weaker its customization and specificity will be (this is easy to understand). The main disadvantage of the PDO interface API is that it will restrict you from using the MySQL service The client provides all advanced database features. For example, PDO does not allow the use of multi-statement execution supported by MySQL.
In PHP5, PDO currently supports a large number of databases, and will be the default database connection method in PHP6:
1. sqlite
2. mysql
3. pgsql
4. mssql
...
PDO is implemented based on the PHP extension framework, and its source code is under ext/pdo in the PHP source code directory
I emphasize again that PDO is just an interface specification. It does not implement any database functions by itself. Programmers must use a "PDO driver" of a specific database to access a specific database
PHP itself has built-in DBX functions. The DBX module is a database abstraction layer (the "X" in DBX represents the X types of databases it can support). DBX functions allow you to access all DBX supported databases.
dba_replace ( "key" , "This is an example!" , $id );
if( dba_exists ( "key" , $id ))
{
echo dba_fetch ( "key" , $id );
dba_delete ( "key" , $id );
}
dba_close ( $id );
?>
复制代码
3. 后记
以上就是PHP连接数据库的不同方式的学习,通过本文的学习,我们了解到一点
目前PHP开发中主流使用的连接数据库的技术是
1. Mysql扩展API
2. Mysqli扩展API
3. PDO抽象层
下一步希望做的事
1. 研究一下PHP和mysql进行交互的协议驱动的底层原理
2. 尝试编程简单的通信协议驱动
http://www.bkjia.com/PHPjc/762932.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/762932.htmlTechArticleContents 1. Introduction to PHP database driver 2. Different ways of connecting to database with PHP 1. Introduction to PHP database driver The driver is a section Designed to interact with a specific type of database server...
Statement:
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