MySQLi Extend Example

By sean on Dec 10, 2010

This is a simple MySQLi extender example. You instantiate this class just as you would MySQLi.

<?php
include_once('mysqli.extend.class.php');
$mysql = new mysqli_db('dbhost', 'dbuser', 'dbpass', 'dbname');
?>

The multi_fetch_array() function essentially allows you to preform multiple queries. This is particularly handy when needing to call multiple stored procedures concurrently.
Example:

<?php
$m[] = sprintf('CALL fetchUserInfo(\'%s\')', $username);
$m[] = sprintf('CALL fetchUserActivity(\'%s\')', $username);
$result = $mysql->multi_fetch_array( implode(';', $m) );
?>
<?php
class mysqli_db extends mysqli {
  public function fetch_array($sql) {
    $query = parent::query($sql);
    if ($query) {
      while( $result = $query->fetch_assoc() ) $return[] = $result;
      $query->free();
    }
    return ( (isset($return)) ? $return : false );
  } 

  public function multi_fetch_array($sql) {
    if (parent::multi_query($sql)) {
      do {
        if ($result = parent::store_result()) {
          while ($row = $result->fetch_assoc()) $return[] = $row;
          $result->free();
        }
      } while (parent::next_result());
    }
    return $return;
  }
}
?>

Comments

Sign in to comment.
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.