Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use MySQLi instead of MySQL?

Tags:

php

mysql

mysqli

Can someone clarify for me what the advantages and disadvantages of using MySQLi instead of MySQL? Are there situations where I should not use MySQLi? Do I need to configure my server differently in order to use MySQLi? For instance, do I need to upgrade Apache or PHP so I can support MySQLi?

like image 976
Semere Taézaz Sium Avatar asked Jan 17 '12 08:01

Semere Taézaz Sium


People also ask

Is MySQLi better than MySQL?

MySQLi supports both procedural interfaces and object oriented interfaces while MySQL supports only procedural interfaces. MySQLi supports stored procedure but MySQL does not. There is enhanced security and improved debugging features in MySQLi where this is comparatively lagging in MySQL.

Should I use MySQLi MySQL?

Basically, MySQL is the old database driver, and MySQLi is the Improved driver. The "i" stands for "improved" so it is MySQL improved. MySQLi can be done procedural and object-oriented whereas MySQL can only be used procedurally. Mysqli also supports prepared statements which protect from SQL Injection.

Is MySQLi faster than MySQL?

According to all the Google results for benchmarks linked by ceejayoz it looks like MySQL is at least slightly faster than MySQLi in all the benchmark tests.

Can I use both MySQL and MySQLi?

It is possible to include both MySQL and MySQLi when connecting to a single database, but it is incredibly delicate and with large amounts of data being passed through it can get very messy and hard to control. it is best to use MySQLi in general in my opinion because it is much more secure and up to date.


2 Answers

Reasons why you should use MySQLi extension instead of the MySQL extension are many:

  1. MySQLi gives you prepared statements - a safer way of sending data to MySQL and protecting you from SQL injection. This alone should be enough for always choosing MySQLi over MySQL.
  2. MySQLi enables most of the MySQL features.
  3. MySQLi is object orientated.
  4. MySQLi supports prepared statements, transactions and multiple statements.
  5. The old MySQL extension is deprecated as of PHP 5.5.0

And there are other benefits. But mainly, you should focus on security and stabiltity - and MySQLi gives you just that.

like image 162
Repox Avatar answered Oct 02 '22 06:10

Repox


PHP team refuse to support mysql extension any further. This reason is alone enough.

All other reasons don't make much sense:

  • MySQLi gives you prepared statements - one can use old mysql with manually handled plaeholders and get even safer. This alone should be stopping MySQL from useless deprecation.
  • MySQLi enables most of the MySQL features which most of PHP users never ever heard of.
  • MySQLi is object oriented - a pair of straight hands can make old mysql objec oriented in a matter of several hours.
  • *MySQLi supports
    • transactions* - mysql supports them as well. just run START TRANSACTION query and you're set.
    • multiple statements - yet nobody actually needs them.
    • prepared statements - as a matter of fact, the support is horrible, renders them practiaclly unusable

So, there are no advantages at all.
If you want to get along with non-deprecated but usable extension - go for the PDO.

like image 42
Your Common Sense Avatar answered Oct 02 '22 07:10

Your Common Sense