user4119626
user4119626

Reputation:

What is the difference between MySQLi and PDO?

I have recently started learning PHP. I came to know that there has been many changes and SQL is now being avoided and has been replaced by MySQLi, but then I came to know about PDO. I have gone through many posts about these two topics, but everyone has their own opinion and I am confused. Many posts have said that MySQLi supports procedural and object oriented method whereas PDO supports only an object-oriented method.

What I want to know is that:

  1. Are these two (PDO and MySQLi) just two different ways of doing the same thing or are they different from each other?

  2. If they are just two different ways to do the same thing then what is the difference between them and which one is a better way to perform the task?

  3. If learning MySQLi, which one is better method between object-oriented and procedural?

For some it may seem to be a broad question, but I would be highly obliged if anyone can give specific answer for these three questions.

Upvotes: 6

Views: 5096

Answers (2)

Sverri M. Olsen
Sverri M. Olsen

Reputation: 13283

PDO is an interface for accessing databases:

The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP. Each database driver that implements the PDO interface can expose database-specific features as regular extension functions. 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. (source)

MySQLi is an extension for accessing MySQL databases:

The mysqli extension allows you to access the functionality provided by MySQL 4.1 and above. (source)

Which one you should use is primarily opinion-based and not particularly well suited for the Stack Overflow format.

Upvotes: 1

Praveen Kushwah
Praveen Kushwah

Reputation: 31

When accessing a database in PHP, we have two choices: MySQLi and PDO. So what should you know before choosing one?

  1. PDO has 12 different driver supports while MYSQLI has only one i.e MYSQL.
  2. PDO uses OOP API while MySqli use both OOP and precedural
  3. PDO has named parameters but MuSqli does not.
  4. With PDO one can have prepared statements at client side

Upvotes: 3

Related Questions