开发者

Switch to mysqli or stay with mysql?

开发者 https://www.devze.com 2023-03-30 20:08 出处:网络
We have an app here that has been in development (and now in production) for more than a year. Which has in total over 500 mysql_* calls.

We have an app here that has been in development (and now in production) for more than a year. Which has in total over 500 mysql_* calls.

Is it worth it to switch all of the mysql_* in the code to mysqli_*

Is it worth chasing all the bugs that might (and most probably will) come about?

I see from questions like this: Changing this from MySQL to MySQLi? that just adding i after every mysql* call can lead me to alot of errors. Is it worth my time?

mysql_* will probably around for the long haul (even amongst rumors of deprecation), so it it really worth any pro开发者_如何学编程grammers time to methodically switch over?

See also this discussion


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/


In my opinion, the benefit of MySQLi is when it is used in an object-oriented fashion, and with prepared statements. You get some additional versatility from it using the procedural style too, such as nice wrapper functions around transaction handling, but I think not enough to justify unless you rewrite lots of your code to make use of them.

And if you were to undertake the effort to convert to OO code or prepared statements, you might as well convert to the more flexible PDO instead of to MySQLi.

Update Jan 2013

Just found this old answer, and in the Aug 2011 comment thread below I said it wasn't worth it to convert mysql_query() calls to mysqli_query() absent an accompanying move to prepared statements. It now IS necessary to start moving in that direction, as the mysql_*() extension is deprecated as of PHP 5.5 and will eventually be removed.


MySQLi has some performance benefits over MySQL. Actually it's recommended to use MySQLi instead of MySQL. You can do the procedural style as well.

You could create a new branch of your app and change the code to mysqli_* functions. This should be pretty straight forward and while doing so you would review your database access code which might help in going on after the switch to mysqli to continue refactoring. If it's all too much hassle, you already benefit from the improved version of the client library in your database access code.


Deprecation of ext/mysql is not a rumor.

However, it is not your real problem.

Your main problem is that you're using naked API calls all over the code instead of using some intelligent library to handle SQL queries.

So, you'd better start to develop such a library or get a ready made one, and then rewrite your code to use it's calls.

Don't you see that all this repeated stuff

$res=mysql_query("SELECT STUFF");
while($row = mysql_fetch){
    $var=$row['col'];
}

being incredibly boring?
why not to use some one-liner like

$data = $db->getRow("SELECT stuff");

which is shorter and can have a lot of features like query logging, counting, debugging, error handling and such?

And, as a side effect of using such a library, the only place where you will need to change whatever API calls will be only this library code.


In my opinion, You should not use mysql_* function directly in your logic. You should write a wrapper. So when you want to switch mysql_* to mysqli_*, you only have to change the code of the wrapper.


If you have that many calls then I suggest you start by implementing (selecting or building) an abstraction layer for your database calls and then convert that.

The biggest benefit (a substantial one) in my similar exercise was the fact that mysqli offers parameterized statements allowing you to leave mysql_real_escape_string etc. behind. But the map from one to the other isn't nearly one-to-one.


Advice: If you're going to do a switch, do not switch to mysqli, instead switch to using PDO. If you must switch to mysqli, make sure you use it in the PHP5 OO way. Don't use mysqli OO and old mysql at the same time or the old mysql and PDO at the same time unless you like unexpected connection squashing.

0

精彩评论

暂无评论...
验证码 换一张
取 消