-2

I'm currently using mysql_query() to approach DB.

I understand that this method will be removed in the future so I want to learn a new api.

What in your opinion is the best way and why? PDO or mysqli? or there is another one I haven't heard about?

Thank you.

OfirH
  • 653
  • 1
  • 7
  • 19
  • PDO because it doesn't restrict you to MySQL. – Sbls Mar 09 '14 at 19:25
  • Personally, I think you should use PDO. It only has one interface (the OOP one) instead of two like MySQLi. and is database agnostic. The PHP core devs are continuing their trend of being morons by providing not one but _two_ successors to `mysql_*` – Bojangles Mar 09 '14 at 19:25
  • 1
    http://stackoverflow.com/questions/11587741/choosing-between-pdo-mysqli-and-traditional-mysql?rq=1 http://stackoverflow.com/questions/13569/mysqli-or-pdo-what-are-the-pros-and-cons?lq=1 – Martin Strouhal Mar 09 '14 at 19:25
  • http://code.tutsplus.com/tutorials/pdo-vs-mysqli-which-should-you-use--net-24059 – felipsmartins Mar 09 '14 at 19:26
  • PDO offers robust protection against sql injection using prepared statements, and also allows you to return results as objects which I like – andrew Mar 09 '14 at 19:27

1 Answers1

2

I'm going to offer what I consider to be the best way to handle database stuff: Build your own class. In my case, this class just wraps mysql_* functions, however if I wanted to it would be really easy to change to a different one (such as if mysql_* gets removed). I only have to change a single file, and instantly the entire project is using a new API.

Believe me, do this right at the start and you will save yourself a LOT of work if and when you decide to change things around!

Niet the Dark Absol
  • 301,028
  • 70
  • 427
  • 540
  • +1, yep I do the same thing. It's all very well saying "you should use PDO" and "mysql is deprecated" when you still have to work with with some site's old php version. – TonyWilk Mar 09 '14 at 19:31