Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 980
ʎɹnɔɹǝW Avatar asked Jan 23 '17 17:01

ʎɹnɔɹǝW


1 Answers

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.

like image 71
Don't Panic Avatar answered Oct 11 '22 13:10

Don't Panic