0
votes

In PHP PDO(PHP Data Objects) tutorials I've been reading that the advantage of PDO over MySQLi is that PDO is platform independent. That said means you wrote a script with PHP PDO using MySQL database management system. Later you want to switch your web application to another Database Management System like Oracle, you will not need to rewrite your queries. While in case of MySQLi you had to rewrite your queries.

Now I'm confused by looking at the following line

$conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);

Why do we need to mention the "mysql" in the first parameter? And if I've to port my website to another DBMS, would I not need to replace this "mysql" with something like "oracle"?

Hopefully somebody clarify this.

Thanks

2
Why do we need to mention the "mysql" in the first parameter? I believe we need to tell PDO which dbms we are using, therefore we can be able to use functions of that dbmsMasivuye Cokile
Using PDO does not guarantee that you do not have to rewrite your queries if you swift to another database. If you use any feature in your queries that is not available in the other rdbms, then your query has to be rewritten.Shadow

2 Answers

2
votes

That's actually a good question, dunno why it was so fiercely downvoted.

Later you want to switch your web application to another Database Management System like Oracle, you will not need to rewrite your queries.

Unfortunately, that's but a nasty rumor. In fact, you'll have to rewrite many queries as well. PDO is just a Database Access Abstraction layer, means it offers the unified API to access different databases, but it doesn't rewrite SQL queries for you according to different SQL flavors.

Why do we need to mention the "mysql" in the first parameter?

Well, even for your imaginary PDO you'd have to tell somehow, which driver to use. So for the actual PDO as well. It is true that a single wending machine can serve you with either Pepsi or Coke, but it cannot read minds and you have to specify what it would be.

So yes - you have to specify the database you are connecting to and rewrite your queries.

the advantage of PDO over MySQLi

That's the most insignificant advantage - there is actually much more.

1
votes

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.

It means you don't have to change your PHP code on DBMS switch, well in most cases, if used correctly. But PDO definitely should know what database you use and how to handle it, driver to use.

Database and type can be safely moved into a config file for later customization.