“PHP Warning: mysqli_* expects exactly 2 parameters, 1 given” while converting from the old MySQL extension to MySQLi

So, you are trying to migrate a codebase from the deprecated MySQL extension to MySQLi. You’ve carefully changed all the mysql_* function calls to the corresponding mysqli_*. You run your code and….

Your data access code is not working anymore and your output is now littered with warnings like:

PHP Warning:  mysqli_select_db() expects exactly 2 parameters, 1 given in...

PHP Warning:  mysqli_query() expects exactly 2 parameters, 1 given in...

PHP Warning:  mysqli_real_escape_string() expects exactly 2 parameters, 1 given in...

What exactly is going on here, and what can you do about it?

The short answer is that, even though the functions of the MySQL extension and MySQLi are very similar, there are subtle but important differences in the signatures of some of the functions.

More specifically, the common $link_identifier parameter which is optional in ext MySQL is required in ext MySQLi.

image/svg+xml mysql_query( $query); mysqli_query( $link, $query); ?

Also, the $link_identifier has moved to be the first parameter in some functions where it used to be the last.

image/svg+xml mysql_query( $query, $link_identifier); mysqli_query( $link, $query);

For a more detailed answer, including some other problems you need to be on the lookout for when converting to MySQLi, see: Hidden Reasons Why Migrating from MySQL to MySQLi Procedural may not be as Simple as You Think

Did this help you?

Sign up for my newsletter and I will send you regular articles and other usefull stuff.

Sign Me Up!

You can unsubscribe at any time

Share this:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.