The Four Major Benefits of MySQLi

A coworker and I were discussing the benefits and drawbacks of mysqli this week. I found it to be very convenient timing considering all the talk on the blogosphere about mysql vs mysqli.
There still seems to be a lot of confusion over the differences between the two extensions despite the length of time that mysqli has been in the wild. I wanted to briefly review the four major benefits of mysqli.

Prepared Statements
The strongest reason to use mysqli should be the prepared statements. A prepared statement is a special query template sent up to the server for validation and storage. From that point forward, the developer can bind input and output parameters to the prepared statement in order to interact with the server.


$mysqli = new mysqli('localhost','dbuser','passwd','my_db');
$stmt = $mysqli->prepare("select `user_id` from `users` where `employee_id` = ?");

$stmt->bind_param('i',$employee number);
$stmt->execute();

$stmt->bind_result($user_id);

while($stmt->fetch()){
echo “the user id is : $user_id\n”;
}

$stmt->close();
$mysqli->close();

Outside of the preparation itself, this makes for some very speedy querying. If you have a lot of similar queries to run, you will notice a performance boost by submitting the prepared statement once and using using it over and over again for your query blocks. All that aside, the best reason to use prepared statements is that it makes it extremely difficult if not impossible for a sql injection attack to occur because the query the sql statement and the user input no longer interact in the same way they would with a normal mysql function. The query template is sent to the server before any data can be associated with it. Given the continued problems we are still experiencing with sql injection, this could be a life saver if it became a standard.

Secure MySQL connections
This feature speaks for itself. The ability to easily use mysqli_ssl_set() to establish a secure connection by providing certificate details as input parameters. I don’t know why this didn’t make it into the mysql extension, but I am glad to see it now.

Multi query

Using the mysqli_multi_query() method/function a programmer can send multiple semi colon delimited queries at once to the server and use each individual result set that gets returned. I am not sold on this feature (because you can no longer prepate statements) but I like having the ability if desired. I don’t see anything wrong with it, provided input parameters are validated.

Object Oriented Interface

Last but not least, mysqli’s OO interface allows a programmer to extend the mysqli object to serve more specialized purposes. I find this particularly exciting for one very simple reason:
I can separate operational errors (failure to connect, access errors, etc) from development errors like query syntax errors. This way, the errors can be routed to the correct groups for resolution without gunking up the code.

I believe we will be seeing an increased use of mysqli as the need to support PHP4 wanes. If your code is not PHP4 dependent, and you have a version of MySQL greater than 4.1.3, you should take a more in depth look at mysqli.

For further information:

Tags:
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • DZone
  • Technorati
  • StumbleUpon

8 Responses to “The Four Major Benefits of MySQLi”

  1. Casey Wise Says:

    Great and informative article! Thank you for posting this.

  2. PHPDeveloper.org Says:

    Charles Rowe’s Blog: The Four Major Benefits of MySQLi…

  3. developercast.com » Charles Rowe’s Blog: The Four Major Benefits of MySQLi Says:

    […] Rowe shares four reasons/benefits he’s come up with that should make you think about choosing MySQLi over the normal MySQL […]

  4. Charles Rowe dot com » Unbuffered result sets in MySQL? I must have been last to know… Says:

    […] « The Four Major Benefits of MySQLi […]

  5. MySQLi | MrFloris Says:

    […] More details about these benefits can be found here: http://www.charlesrowe.com/2007/06/15/the-four-major-benefits-of-mysqli/ […]

  6. vB Feed » Blog Archive » MySQLi Says:

    […] details about these benefits can be found here: http://www.charlesrowe.com/2007/06/1…its-of-mysqli/ And to save time here’s a great article for experienced developers: […]

  7. vB Feed » Blog Archive » MySQLi Says:

    […] details about these benefits can be found here: http://www.charlesrowe.com/2007/06/1…its-of-mysqli/ And to save time here’s a great article for experienced developers: […]

  8. MySQLi Says:

    […] details about these benefits can be found here: http://www.charlesrowe.com/2007/06/1…its-of-mysqli/ And to save time here’s a great article for experienced developers: […]

Leave a Reply