What is the difference between Mysqlnd, PDO and PDO_Mysql extensions in php?
Asked Answered
P

2

10

They seem to be the same thing, and yet different. I don't know which one is which: I know we can use PDO as new PDO() and use prepare() and query() methods, to fetch data from database. So, if this is PDO mentioned in the extensions list, then what exactly (in laymans terms) are pdo_mysql and mysqlnd?

Palembang answered 23/1, 2017 at 17:56 Comment(11)
See also: php.net/manual/en/mysqlinfo.api.choosing.php and php.net/manual/en/mysqlinfo.library.choosing.phpFokos
I have read it actually, I just can't still make the connection (no pun intended) between the three of themMella
I think (and I'm sure someone will correct me if I'm wrong) that mysqlnd is the actual driver that connects PHP to mysql. PDO and mysqli are the APIs that send data through mysqlnd to the mysql server.Haematoxylin
using PDO in PHP requires the PDO driver, which is what pdo_mysql is. Since we want to connect to a mysql db using PDO, we also need the mysql driver, which is what mysqlnd is.Refute
It sounds like you might not be aware that PDO can be used with many different databases, and not just MySQL. You have to enable the specific extensions for each the type of database you want to use with PDO, (e.g. pdo_mysql for MySQL).Stealing
the bigger difference is that only PDO supports a dozen different databases php.net/manual/en/pdo.drivers.php where mysql_ and mysqli_ only support MySQL.Gardia
Regarding mysqlnd, this document from the PHP manual will probably help you understand what it is about. An important fact you'll find there is that "it does not provide a new API to the PHP programmer".Stealing
Let's assume that mysql (software provided by mysql) is actually running and doesn't depend on PHP, because it's a seperate entity, now I am assuming something connects these two applications, but which one of the three is that? and what about the rest?Mella
PDO and PDO_MySQL are the same thing. mysqlnd sits in between PHP and the mysql server, like a post office, or a truck stop waitress (for some reason, my attempt to create a dialogue made mysqlnd sound like a New Yorker...)Haematoxylin
@Haematoxylin How are PDO and PDO_MySQL the same thing? There must be a difference, or are they the identical applications with different name?Mella
The PDO API is installed by the PDO_mysql package, I think. From the docs, PDO_MYSQL is a driver that implements the PHP Data Objects (PDO) interface to enable access from PHP to MySQL 3.x, 4.x and 5.x databases.Haematoxylin
S
15

PDO itself is a database abstraction layer offered by PHP. This may not mean quite what you think it does, but PHP clears that up in the PDO documentation:

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.

Depending on your system, you might need to enable its driver before you can use it. (But probably not.)

You can use PDO with various different databases (MySQL, Oracle, PostgreSQL, etc.) but you will need to enable the specific driver for the type of database you want to use. From the same PDO introduction documentation I linked earlier:

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_mysql is one of these. These drivers implement the PDO interface, which means that they allow you to use prepare(), query() etc. with your specific database.

mysqlnd handles the communication between PHP and MySQL. It has been the default driver for all MySQL extensions (like pdo_mysql) since PHP version 5.4, so if you're using a supported version of PHP, you're probably using it. But it works behind the scenes, just handling the communication. You won't use any mysqlnd functions. From the "What it is not" section of the mysqlnd documentation:

Although MySQL Native Driver is written as a PHP extension, it is important to note that it does not provide a new API to the PHP programmer. The programmer APIs for MySQL database connectivity are provided by the MySQL extension, mysqli and PDO MYSQL. These extensions can now use the services of MySQL Native Driver to communicate with the MySQL Server. Therefore, you should not think of MySQL Native Driver as an API.

Semitone answered 23/1, 2017 at 18:56 Comment(0)
B
2

You need PDO_Mysql to use a mysql database with PDO. It is the driver.

Mysqld is the Mysql server, which you access using PDO or mysqli.

Mysqlnd is the library/the driver to communicate with the Mysql server, both PDO and mysqli use it.

Beriosova answered 23/1, 2017 at 18:2 Comment(1)
If you're going to answer this, do it right. Show the quotes from the official docs. Also, OP asks about mysqlnd - NOT mysqld. What you've stated so far is marginal at best, and could just be a comment.Refute

© 2022 - 2024 — McMap. All rights reserved.