When Should I Use MySQLi Instead of MySQL

When should I use MySQLi instead of MySQL?

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.

MySQL vs MySQLi when using PHP

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.

Switch to mysqli or stay with mysql?

Quoting the manual for ext/mysqli:

The mysqli extension has a number of benefits, the key enhancements over the mysql extension being:

  • Object-oriented interface
  • Support for Prepared Statements
  • Support for Multiple Statements
  • Support for Transactions
  • Enhanced debugging capabilities
  • Embedded server support

Note: If you are using MySQL versions 4.1.3 or later it is strongly recommended that you use this extension.

If you need just one of those features and can afford the refactoring, then yes, go for it. If you dont need any of those features then dont do it. There is no reason to refactor if there is no benefits.

On a sidenote, the rumors are true. ext/mysql will likely be deprecated (although no one can say when at the time of this writing. It certainly wont be deprecated with 5.4. and it will likely be available as a pecl extension forever) In any case, you shouldnt be starting any new projects with ext/mysql anymore when you have a superior extension to start with.

Also see http://blog.ulf-wendel.de/2012/php-mysql-why-to-upgrade-extmysql/

Updating WordPress to use mysqli instead of mysql

At first, check this tool. It can help to migrate from mysql_* to mysqli_*.

If not, simply extend wpdb:

  1. Write a class with getter for "$dbh":

  2. Use it everywhere instead of base class (this link
    can help)

Use mysqli or PDO instead?

The entire ext/mysql PHP extension was officially deprecated in PHP v5.5.0 and removed in PHP v7.There are two other MySQL extensions that you can consider: MySQLi and PDO_MySQL, either of which can be used instead of ext/mysql.
it's possible to suppress deprecation errors by setting error_reporting in php.ini or in your each pages or config file to exclude E_DEPRECATED.

error_reporting = E_ALL ^ E_DEPRECATED

or

you can use
error_reporting(0)

Change your code From

$con = mysql_connect('localhost', 'user_log', '12345');
to

$con= mysqli_connect($db_host,$db_user,$db_pass,$db_database);

What is the difference between mysql and mysqli in WAMP?

mysqli and PDO, both php APIs for accessing MySQL, are bundled into WAMP. There's nothing else to download. You should be good to go.

Good for you for noticing the deprecation of mysql!

If you're doing this project to learn about dbms technology in general, you might consider working with PDO: you can use the same APIs (with small variations) to connect to other dbmss, including PostgreSQL, Oracle, and MS SQL Server. Even though some of those are expensive commercial products, you can get free evaluation copies for learning purposes to run on your own machine.

How to use mysql insted of mysqli in PHP

http://php.net/manual/en/intro.mysql.php

The mysql_* functions have long been deprecated, and were finally
removed in PHP 7. The mysqli_* functions are quite similar and it should be pretty easy to 'upgrade' a piece of code to use mysqli, even if you don't use the extra features, like bind parameters.

But for a course, even a small difference may cause a lot of confusement, so if you really want to do this particular exam, you could downgrade to a
version prior to PHP 7.0.

The WampServer website still refers to older downloads on SourceForge. You can download WampServer 2 with PHP 5.5. That should work.

You might consider using a virtual machine to install it on, if you also want to keep your currently installed latest version.

But if you have any choice, choose another exam or course, because this one is very outdated.

If you were going to learn a more modern database API, I'd choose PDO. It's just more modern, more flexible and feels less tied to MySQL as its only data source. So in my personal opinion you'd use mysqli to quickly upgrade from mysql, but use PDO for new developments, although opinions on this may vary. :)

Should I have to upgrade my website to PHP MySQLi or PDO?

The answer is fairly simple.

If, like majority of PHP users, you are going to use database API functions right in the application code, without any intermediate wrapper, then PDO is your only choice, as it's a sort of wrapper already, automating many operations that with mysqli have to be done manually.

No, there are no migration options, because the very approach is changed dramatically: instead of placing variables right in the query, they have to be substituted in the query with special marks. There is no way to automate this process.



Related Topics



Leave a reply



Submit