mysqli vs mysql?

I'm reading a PHP book. He is using mysqli_query etc.

Why is that?

I'm using mysql_query. Should I stick to it?
Who is Participating?
Naveed_ManzoorConnect With a Mentor Commented:
MySQLi is new extension whose major goals are
"      Easier maintainability The ext/mysql code base has become somewhat complex and messy. Major enhancements in the functionality of MySQL required that various feature sets be enabled or disabled based on the version of the client library. Other issues required that some features be disabled for particular operating systems.
"      Better compatibility The extension needed to conform more closely to the MySQL client library, so that future enhancements to the library could be supported more easily in PHP
"      Backwards compatibility Although the compatibility between ext/mysql and ext/mysqli is not perfect, effort was made to simplify porting applications from ext/mysql to ext/mysqli.

Major Features
"      A procedural interface that looks very much like the ext/mysql interface
"      An object-oriented interface that allows for a use idiom that is both more convenient and easier to extend than the procedural interface
"      Support for the new MySQL binary protocol that was introduced in MySQL 4.1. (The new protocol is more efficient that the old one and allows for the support of a broader range of features, such as prepared statements.)
"      Support for the full feature set of the MySQL C client library, including the ability to set advanced connection options via mysqli_init() and related functions.
Additionally, the extension has support for additional tracing debugging, load balancing and replication functionality

Why Switch?
Beyond gaining access to the new features of MySQL 4.1+, why would anyone want to switch to using ext/mysqli?

In addition to the functionality mentioned above, ext/mysqli also has some other serious benefits:
"      Greater speed. Enhancements in both the extension and in MySQL have made most operations faster, with certain operations becoming up to 40 times faster as compared to ext/mysql.
"      Better security. In older versions of the MySQL RDBMS, the possibility existed for an attacker to extract weak password hashes from the network and then recreate a user's password. The new authentication procedure is much more robust and mirrors the attack-resistant authentication procedure of tools like SSH.

Still not able to find way to echo or debug prepared statements .

szewkamConnect With a Mentor Commented:
read this, it should clarify :)
Roger BaklundCommented:
The "i" in mysqli stands for "improved". mysqli is a newer implementation of the mysql client library in php.

mysqli have some features not available in the standard php "mysql" client library. Most notably, it is object oriented and supports prepared statements. Using prepared statements is more secure.

Most tutorials and php/mysql scripts you find on the net will be using the older mysql_* functions. This is one reason to stick with this.

Except for the improvements in mysqli, the two library implementations are quite similar. Most of the old mysql_ functions also exists as a mysqli_ function. Learning to use one of these will to some degree also teach you to use the other.

My advice: learn both. Focus on mysqli, but experiment with the classic mysql_ functions also, so that you know how to use it and how to modify old code you might come across.
MySQLi requires an additional PHP extension.

The only differences I know of are that MySQLi has multi_query, you have to give the link resource in each query, and it escapes strings for you.

I have read in many different places that MySQLi is faster, but the test results I see point to MySQL.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.