Saturday, April 20, 2019

php - why we write mysql in $conn = new pdo(); if pdo is platform independent



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


Answer




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.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...