MySQLHandler

MySQLHandler is a complete wrapper for MySQL. It implements all functions available and can be easily customized for your preference.

Notice that MySQLHandler is a package of class files and demands higher knowledge of PHP and MySQL. It is not suitable for the beginner, and I will not educate anyone.

Features

When using a SELECT query, the rows can be returned in 3 different formats

  1. Standard ResultSet
  2. 2-dimensional array
  3. XML

Using the XML return method is very usable if you wish to export the results from a query to another application. Using XSLT and CSS you can also present it as a pretty HTML page.

Requirements

  • Runs on both windows and Linux based platforms
  • PHP 5.x
  • MySQL

Installation

Edit the configuration variables in the file called *Database.class.php*

PHP:
  1. <?php
  2. private $server   = 'localhost'; // Server address
  3. private $database = 'database'; // Name of database
  4. private $username = 'user'; // username
  5. private $password = 'pass'; // password
  6. private $use_permanent_connection = false;
  7. private $xslt_file = ''; // Optional XSLT file
  8. private $xml_encoding = 'ISO-8859-1'; // Char-set encoding on XML
  9. ?>

For improved security, place all class files outside the public_html directory.

Usage

The following example will demonstrate how to use the package.

PHP:
  1. <?php
  2. require_once('ExceptionHandler.class.php');
  3. require_once('MySqlInterface.interface.php');
  4. require_once('MySQLHandler.class.php');
  5. require_once('Database.class.php');
  6.  
  7. $Database = new Database();
  8.  
  9. $data = $Database->sqlQuery("SELECT * FROM table");
  10. print_r($data);
  11. ?>

You can look through the *Database.class.php* file and read the public functions by yourself to learn more on how to use it.

Updates

  • Version 2.1
    • Now uses DOM for creating an XML result.
    • ExceptionHandler is now static

Todo

  • Option to log errors to a file
  • Read settings from an XML-file

Download

Visit the Google Code Project Page for download

5 Responses to “MySQLHandler”
  1. m@ Says:

    Very cool!

    Question: When I output an array with print_r($data);

    It’s giving duplicate keys, how can I stop that?

    Example output:

    Array
    (
    [0] => Array
    (
    [0] => 1
    [id] => 1
    [1] => 2
    [type_id] => 2
    )

    )

  2. SubZane Says:

    Hi, you can ignore that if you want and just go by using the literal keys like so:
    $data[0][’type_id’]

    However, I’m working on removing the duplicates.

  3. subzane.com » Blog Archive » MySQLHandler 2.1 Says:

    […] MySQLHandler […]

  4. drugba Says:

    why?… I have this error:

    Fatal error: Interface ‘MySqlInterface’ not found in /www/sites/1/iespana.es/i/n/institutoeasl/site/SZUserMgnt/MySQLHandler.class.php on line 32

    please please help

  5. Oscar lara Says:

    i get this message in my log in process.
    Parse
    error: syntax error, unexpected T_STRING, expecting ‘{’ in /home/edific2/public_html/pryavalencia/inc/MySQLHandler.class.php on line 32
    in line 32 the script goes
    “class MySqlHandler implements MySqlInterface {”
    Why asks for a “{”?
    please help

Leave a Reply

You must be logged in to post a comment.