PHP5 provides a new set of improved and optimized variants of mysql functions. Typically, the names of these functions start with "mysqli_" instead of "mysql_"; the letter "i" in the name probably stands for "improved"! In this page, we briefly describe a handful of these optimized functions.

Some of the optimized variants have a new signature: mysqli_connect(), mysqli_query(), and mysqli_select_db(). Here is their changed signature:

 $rid       = mysqli_connect($db_host, $user, $password, $db_name);
 $query_rid = mysqli_query($rid, $query);
 $ret_val   = mysqli_select_db($rid, $database_name);

mysqli_connect() is the new improved equivalent to mysql_connect(). The notable difference between mysqli_connect() and mysql_connect() is that the newer version also takes the name of database. Thus, mysqli_connect() is equivalent to two steps: mysql_connect() and mysql_select_db(). Its return value is a resource id that can be used with subsequent functions.

Likewise, mysqli_query() is the new improved variant of mysql_query(); unlike mysql_query(), mysqli_query() also take the resource id returned from mysqli_connect().

Along the same lines, mysqli_select_db() also accepts the resource id returned from mysqli_connect() step.

Some of the improved functions continue to retain their old-style function signature. These functions are as follows:

 $num_rows  = mysqli_num_rows($result);
 $row       = mysqli_fetch_row($result);
 $result    = mysqli_close($db);
 $errno     = mysqli_errno();
 $error     = mysqli_error();

Lastly, the improved function-set introduces two new functions to handle failure during the connect step. For non-connect steps, we can continue to use mysqli_errno() and mysqli_error() functions. These two new functions are:

 $errno     = mysqli_connect_errno();
 $error     = mysqli_connect_error();

comments powered by Disqus