171

Which is better, MySQL or MySQLi? And why? Which should I use?

I mean better not just in terms of performance, but any other relevant feature.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Anand
  • 7,654
  • 9
  • 46
  • 60

6 Answers6

114

If you have a look at MySQL Improved Extension Overview, it should tell you everything you need to know about the differences between the two.

The main useful features are:

  • an Object-oriented interface
  • support for prepared statements
  • support for multiple statements
  • support for transactions
  • enhanced debugging capabilities
  • embedded server support.
Tharif
  • 13,794
  • 9
  • 55
  • 77
Mark Davidson
  • 5,503
  • 5
  • 35
  • 54
  • 1
    yeah, the best thing is that mysqli supports prepared statements – jondinham Oct 22 '11 at 09:57
  • 8
    It should also be noted that MySQLi only works with MySQL 5+. This isn't really relevant anymore, but when MySQLi came out, MySQL 4 was still the standard. This is part of the reason the extensions are separate, the old MySQL driver staying there for compatibility purposes. – zneak Jan 04 '13 at 05:37
  • 8
    It's worth noting that things have changed a lot in six years. `mysql_*()` is now deprecated and __will be removed__ soon. You shouldn't use it for new code. –  May 21 '15 at 01:47
  • Which should we choose with less CPU and RAM? – Mahdi Jazini Oct 05 '15 at 06:36
  • 1
    See Gordon's answer below. The old mysql extension is no longer supported so you'll be better of using mysqli extension anyway. – Mark Davidson Oct 05 '15 at 09:37
  • @user1864610 It was removed with PHP 7 – ReinstateMonica3167040 Aug 04 '17 at 22:51
71

There is a manual page dedicated to help choosing between mysql, mysqli and PDO at

The PHP team recommends mysqli or PDO_MySQL for new development:

It is recommended to use either the mysqli or PDO_MySQL extensions. It is not recommended to use the old mysql extension for new development. A detailed feature comparison matrix is provided below. The overall performance of all three extensions is considered to be about the same. Although the performance of the extension contributes only a fraction of the total run time of a PHP web request. Often, the impact is as low as 0.1%.

The page also has a feature matrix comparing the extension APIs. The main differences between mysqli and mysql API are as follows:

                               mysqli     mysql
Development Status             Active     Maintenance only
Lifecycle                      Active     Long Term Deprecation Announced*
Recommended                    Yes        No
OOP API                        Yes        No
Asynchronous Queries           Yes        No
Server-Side Prep. Statements   Yes        No
Stored Procedures              Yes        No
Multiple Statements            Yes        No
Transactions                   Yes        No
MySQL 5.1+ functionality       Yes        No

* http://news.php.net/php.internals/53799

There is an additional feature matrix comparing the libraries (new mysqlnd versus libmysql) at

and a very thorough blog article at

Gordon
  • 312,688
  • 75
  • 539
  • 559
15

I have abandoned using mysqli. It is simply too unstable. I've had queries that crash PHP using mysqli but work just fine with the mysql package. Also mysqli crashes on LONGTEXT columns. This bug has been raised in various forms since at least 2005 and remains broken. I'd honestly like to use prepared statements but mysqli just isn't reliable enough (and noone seems to bother fixing it). If you really want prepared statements go with PDO.

cletus
  • 616,129
  • 168
  • 910
  • 942
13

MySQLi stands for MySQL improved. It's an object-oriented interface to the MySQL bindings which makes things easier to use. It also offers support for prepared statements (which are very useful). If you're on PHP 5 use MySQLi.

Ross
  • 46,186
  • 39
  • 120
  • 173
5

What is better is PDO; it's a less crufty interface and also provides the same features as MySQLi.

Using prepared statements is good because it eliminates SQL injection possibilities; using server-side prepared statements is bad because it increases the number of round-trips.

MarkR
  • 62,604
  • 14
  • 116
  • 151
3

for me, prepared statements is a must-have feature. more exactly, parameter binding (which only works on prepared statements). it's the only really sane way to insert strings into SQL commands. i really don't trust the 'escaping' functions. the DB connection is a binary protocol, why use an ASCII-limited sub-protocol for parameters?

Javier
  • 60,510
  • 8
  • 78
  • 126
  • 1
    PDO usually uses client-side prepared statements, so they aren't really prepared on the server - but this is good, as it saves server resources and usually performs better. The prepared statement "emulation" will always escape things correctly. – MarkR Feb 15 '09 at 13:22
  • 1
    "always" and "escaping" are dangerous words when go together. i don't know, maybe this code is totally bug-free; but why bother, when a real binary protocol is available? as for performance, that's open to benchmarking. – Javier Feb 16 '09 at 12:34