MySQL Improved

New with PHP 5 is the MySQLi extension, which is "MySQL Improved." This is an all new extension designed to take advantage of the new features available from MySQL 4.1 and upward, and includes new functionality such as native commit and rollback, as well as prepared statements. As the MySQLi extension is only designed to work with MySQL 4.1 and upward, it isn't likely to see any widespread use for some time.

If you are an early adopter of MySQL 4.1 and want to jump in headfirst with some testing, the MySQLi functions work similarly to the MySQL functions—you just need to add an "i" after "mysql" in your code. For example, mysql_connect() becomes mysqli_connect(), mysql_query() becomes mysqli_query(), etc. That said, there are some differences between MySQL and MySQLi code. For example, mysqli_connect()'s fourth parameter lets you specify the default database to use, letting you skip the call to mysqli_select_db(). If you still want to use it, mysqli_select_db() itself is also different, now taking the return value of mysqli_connect() as its first parameter, and the database to select as its second parameter.

Warning

At the time of writing, three MySQLi functions had potentially serious incompatibilities with their MySQL cousins. All three of mysqli_fetch_row(), mysqli_fetch_array(), and mysqli_fetch_assoc() return null when there are no more rows to be found, as opposed to the false that the MySQL extension would have returned. If you want to keep your code easily ...

Get PHP in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.