MySQL Advanced Class (MySQLa)

[ Features | Compare to MySQLi | Instructions | Download | History | Links ]
{ PHP / Personal Project / Summer 2007 }

In the Summer of 2007, I was working on a vote-based music streaming server in PHP when it occurred to me that opening, querying, and closing MySQL connections was a real pain in the ass (especially if you forget to close them or tried to use one when a query is already in progress). So I created a basic PHP class that automated handling opening and closing multiple connections. Over time, it evolved to include query processing, parameter binding, return value iterating, and even static accessing.

Features

Here is a list of basic features/improvements over PHP’s built-in MySQL Improved Class.

  1. No object instantiations needed.
  2. Single line access to any query.
  3. Built in parameter binding.
  4. Results are Object, Numeric, and Array accessible.
  5. Accessible from anywhere in the code (no passing parameters or using globals).
  6. Store multiple connections but only connect to those you use.
  7. Automagically opens and closes connections when used.
  8. Throws exceptions instead of requiring additional function calls for error codes

Comparison to MySQLi

This new class simplifies and secures the way you access MySQL databases. Take a look at this example of using the MySQLi (Object-Oriented Version) from PHP.net

  1. $mysqli = new mysqli("localhost", "my_user", "my_password", "world");
  2.  
  3. /* check connection */
  4. if (mysqli_connect_errno()) {
  5.   printf("Connect failed: %s\n", mysqli_connect_error());
  6.   exit();
  7. }
  8.  
  9. $query = "SELECT Name, CountryCode FROM City ".
  10.          "ORDER by ID DESC LIMIT 150,5";
  11.  
  12. if ($stmt = $mysqli->prepare($query)) {
  13.   /* execute statement */
  14.   $stmt->execute();
  15.  
  16.   /* bind result variables */
  17.   $stmt->bind_result($name, $code);
  18.  
  19.   /* fetch values */
  20.   while ($stmt->fetch()) {
  21.     printf ("%s (%s)\n", $name, $code);
  22.   }
  23.  
  24.   /* close statement */
  25.   $stmt->close();
  26. }
  27.  
  28. /* close connection */
  29. $mysqli->close();

Now that was ~12 lines of actual code, 5 of which were used to execute the query. If we were to replicate that with the MySQLa class, it would look like this.

  1. try {
  2.   MySQLa::addServer("localhost", "my_user", "my_password", "world");
  3.  
  4.   // Return all the rows
  5.   $rows = MySQLa::query("SELECT Name, CountryCode FROM City ".
  6.                         "ORDER by ID DESC LIMIT 150,5",
  7.                          MySQLa::RETURN_ALL)->fetch();
  8.  
  9.   // Iterate through the rows
  10.   foreach ($rows as $row) {
  11.     printf ("%s (%s)\n", $row->Name, $row->CountryCode);
  12.   }
  13. } catch (MySQLa_Exception $ex) {
  14.   // Check for any errors in Connection, Query, or Result.
  15.   echo "MySQLa Exception [".$ex->getCode()."]: ".$ex->getMessage();
  16. }

This is only about ~6 lines of code. And the query itself was only 1 line.

This new class offers the ability to execute queries from within any function, without the worry of opening/closing queries, results, or even connections.

Instructions

Examples of all of these are in the “tutorial.php” file inside the attached ZIP file.

  • Adding Connections (Nickname is for the Connection)
    1. // Single-Connection Use
    2. MySQLa::addServer($ipaddress, $username, $password, $schema);
    3. // Multiple-Connection Use
    4. MySQLa::addServer($nickname, $ipaddress, $username, $password, $schema);
  • Making Basic Query
    1. // Single-Connection Use
    2. $row = MySQLa::query($query_string, $return_type)->fetch();
    3. // Multiple-Connection Use
    4. $row = MySQLa::query($nickname, $query_string, $return_type)->fetch();
  • Return Types: (Constants)
    1. MySQLa::RETURN_NULL // Returns nothing (default)
    2. MySQLa::RETURN_VALUE // Returns a single value
    3. MySQLa::RETURN_ROW // Returns a whole row of data (can be iterated)
    4. MySQLa::RETURN_ALL // Returns array of rows
    5. MySQLa::RETURN_INSERT_ID // Returns the last insert ID
    6. MySQLa::RETURN_ROWS_AFFECTED // Returns number of rows affected
  • Parameter Binding
    1. // Binding Value should be:
    2. // array($types, $var1, $var2, …)
    3. // and $types is a string of i, d, s, b (see mysqli_stmt::bind_param)
    4.  
    5. // Single-Connection Use
    6. $row = MySQLa::query($query_string, $binding_value, $return_type);
    7. // Multiple-Connection Use
    8. $row = MySQLa::query($nickname, $query_string, $binding_value, $return_type);

History

  • 23 April 2010 – r136 – Updated Tutorial (included SQL file)
  • 21 April 2010 – r135 – First Release to Public

Download

Links/Resources

Leave a Reply

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



  • Donate

    If my work has helped you and you want to return the favor, you could purchase something for me from my Amazon Wish List or send me a donation via PayPal.

  • License

    Unless otherwise noted, all source code and compiled files published on this website are released under the terms of the GNU Lesser General Public License.