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
- Standard ResultSet
- 2-dimensional array
- 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
-
private $server = 'localhost'; // Server address
-
private $database = 'database'; // Name of database
-
private $username = 'user'; // username
-
private $password = 'pass'; // password
-
private $use_permanent_connection = false;
-
private $xslt_file = ''; // Optional XSLT file
-
private $xml_encoding = 'ISO-8859-1'; // Char-set encoding on XML
-
?>
For improved security, place all class files outside the public_html directory.
Usage
The following example will demonstrate how to use the package.
-
<?php
-
require_once('ExceptionHandler.class.php');
-
require_once('MySqlInterface.interface.php');
-
require_once('MySQLHandler.class.php');
-
require_once('Database.class.php');
-
-
$Database = new Database();
-
-
$data = $Database->sqlQuery("SELECT * FROM table");
-
?>
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
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
)
)
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.
[…] MySQLHandler […]
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
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