PHP Classes

Zebra Database: MySQL database access wrapper library

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStar 49%Total: 2,087 All time: 1,877 This week: 455Up
Version License PHP version Categories
zebra-database 2.9.0GNU Lesser Genera...5.0.4Databases
Description 

Author

Zebra_Database it is an advanced, compact (one-file only), lightweight, object-oriented MySQL database wrapper built upon PHP's MySQL extension. It provides methods for interacting with MySQL databases that are more intuitive and fun to use than PHP's default ones.

The class provides a comprehensive debugging interface with detailed information about the executed queries: execution time, returned/affected rows, excerpts of the found rows, error messages, etc. It also automatically EXPLAIN's each SELECT query (so you don't miss those keys again!).

It encourages developers to write maintainable code and provides a better default security layer by automatically escaping strings.

Zebra_Database's code is heavily commented and generates no warnings/errors/notices when PHP's error reporting level is set to E_ALL.

Picture of Stefan Gabos
Name: Stefan Gabos <contact>
Classes: 12 packages by
Country: Romania Romania
Age: 44
All time rank: 312 in Romania Romania
Week rank: 206 Down3 in Romania Romania Down
Innovation award
Innovation award
Nominee: 2x

Winner: 1x

Example

<!DOCTYPE html>

<html>

    <head>

        <meta charset="utf-8">

        <title>Zebra_Database Example</title>

    </head>

    <body>

    <p>
        Prior to running this example you must first download and install the <a href="http://dev.mysql.com/doc/index-other.html">
        <em>world</em></a> test database from MySQL's website.<br>
        Then, you must edit this file (example.php) and change the settings of <em>host</em>, <em>user name</em>, <em>password</em>
        and <em>database</em> to match your configuration.
    </p>

    <?php

       
// THIS EXAMPLE IS VERY BRIEF!
        // CHECK THE DOCUMENTATION TO SEE WHAT METHODS ARE AVAILABLE!

        // include the wrapper class
       
require '../Zebra_Database.php';

       
// create a new database wrapper object
       
$db = new Zebra_Database();

       
// turn debugging on
       
$db->debug = true;

       
// connect to the MySQL server and select the database
       
$db->connect(
           
'', // host
           
'', // user name
           
'', // password
           
'' // database
       
);

       
$db->set_charset();

       
// let's work with a country
       
$country = 'Romania';

       
// get the country's code
       
$country_code = $db->dlookup('Code', 'country', 'Name = ?', array($country));

       
// get all the cities for the country code
       
$db->query('
            SELECT
                Name
            FROM
                city
            WHERE
                CountryCode = ?
            ORDER BY
                Name
        '
, array($country_code));

       
// get all the languages spoken for the country code
       
$db->query('
            SELECT
                Language,
                IsOfficial,
                Percentage
            FROM
                countrylanguage
            WHERE
                CountryCode = ?
            ORDER BY
                Percentage DESC
        '
, array($country_code));

       
// show debug console.
        // THIS SHOULD ALWAYS BE PRESENT AT THE END OF YOUR SCRIPTS!
        // debugging should be controlled by setting the "debug" property to TRUE/FALSE
       
$db->show_debug_console();

   
?>

    </body>

</html>


Details

Zebra_Database

An advanced, compact and lightweight MySQL database wrapper library, built around PHP's mysqli extension

It provides methods for interacting with MySQL databases that are more powerful and intuitive than PHP?s default ones.

It supports transactions and provides ways for caching query results either by saving cached data on the disk, or by using memcache.

The class provides a comprehensive debugging interface with detailed information about the executed queries: execution time, returned/affected rows, excerpts of the found rows, error messages, etc. It also automatically EXPLAIN?s each SELECT query (so you don?t miss those keys again!).

It encourages developers to write maintainable code and provides a better default security layer by encouraging the use of prepared statements, where parameters are automatically escaped.

Zebra_Database?s code is heavily commented and generates no warnings/errors/notices when PHP?s error reporting level is set to E_ALL.

Features

  • it uses the mysqli extension for communicating with the database instead of the old mysql extension, which is officially deprecated as of PHP v5.5.0 and will be removed in the future; again, this is not a wrapper for the PDO extension which is already a wrapper in itself
  • offers lots of powerful methods for easier interaction with MySQL
  • provides a better security layer by encouraging the use of prepared statements, where parameters are automatically escaped
  • provides a very detailed debugging interface with lots of useful information about executed queries; it also automatically EXAPLAIN?s each SELECT query
  • supports caching of query results to disk or to a memcache server
  • has comprehensive documentation
  • code is heavily commented and generates no warnings/errors/notices when PHP?s error reporting level is set to E_ALL

Requirements

PHP 5+ with the mysqli extension activated, MySQL 4.1.22+

For using memcache as caching method, PHP must be compiled with the memcache extension and, if memcache_compressed property is set to TRUE, needs to be configured with ?with-zlib[=DIR]

How to use

Connect to a database


<?php

require 'path/to/Zebra_Database.php';

$db = new Zebra_Database();

// turn debugging on
$db->debug = true;

// set relative path to parent of public folder from $_SERVER'DOCUMENT_ROOT'
// no leading slash
// ie: http://example.com/vendor/stefangabos/zebra_database/public/css/database.css
$db->resource_path = 'vendor/stefangabos/zebra_database';

$db->connect('host', 'username', 'password', 'database');

// code goes here

// this should always be present at the end of your scripts;
// whether it should output anything should be controlled by the $debug property
$db->show_debug_console();

?>

A SELECT statement

<?php

// $criteria will be escaped and enclosed in grave accents, and will
// replace the corresponding ? (question mark) automatically
$db->select(
    'column1, column2',
    'table',
    'criteria = ?',
    array($criteria)
);

// after this, one of the "fetch" methods can be run:

// to fetch all records to one associative array
$records = $db->fetch_assoc_all();

// or fetch records one by one, as associative arrays
while ($row = $db->fetch_assoc()) {
    // do stuff
}
?>

An INSERT statement

<?php

$db->insert(
    'table',
    array(
        'column1' => $value1,
        'column2' => $value2,
    )
);

?>

An UPDATE statement

<?php

// $criteria will be escaped and enclosed in grave accents, and will
// replace the corresponding ? (question mark) automatically
$db->update(
    'table',
    array(
        'column1' => $value1,
        'column2' => $value2,
    ),
    'criteria = ?',
    array($criteria)
);

?>

Visit the project's homepage for more information.


  Files folder image Files (31)  
File Role Description
Files folder imageexamples (3 files)
Files folder imagelanguages (4 files)
Files folder imagepublic (1 file, 2 directories)
Accessible without login Plain text file changelog.txt Data Changelog
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file documentation.txt Data Documentation
Accessible without login Plain text file index.html Conf. Prevent directory listing
Accessible without login Plain text file license.txt Lic. License
Accessible without login Plain text file README.md Data Auxiliary data
Accessible without login Plain text file readme.txt Data Readme File
Plain text file Zebra_Database.php Class Class source

  Files folder image Files (31)  /  examples  
File Role Description
  Accessible without login Plain text file example.php Example Example script
  Accessible without login HTML file index.html Conf. Prevent directory listing
  Accessible without login Plain text file install.txt Doc. Documentation

  Files folder image Files (31)  /  languages  
File Role Description
  Accessible without login Plain text file english.php Data Language file
  Accessible without login Plain text file german.php Aux. Auxiliary script
  Accessible without login HTML file index.html Conf. Prevent directory listing
  Accessible without login Plain text file russian.php Aux. Auxiliary script

  Files folder image Files (31)  /  public  
File Role Description
Files folder imagecss (12 files)
Files folder imagejavascript (3 files)
  Accessible without login HTML file index.html Conf. Prevent directory listing

  Files folder image Files (31)  /  public  /  css  
File Role Description
  Accessible without login Image file affected.png Icon Image file
  Accessible without login Image file backtrace.png Icon Image file
  Accessible without login Image file cache.png Icon Image file
  Accessible without login Image file clock.png Icon Image file
  Accessible without login Image file close.png Icon Image file
  Accessible without login Plain text file database.css Aux. CSS file
  Accessible without login Image file db.gif Icon Image file
  Accessible without login Image file explain.png Icon Image file
  Accessible without login HTML file index.html Conf. Prevent directory listing
  Accessible without login Plain text file license.txt Lic. License
  Accessible without login Image file records.png Icon Image file
  Accessible without login Image file top.png Icon Image file

  Files folder image Files (31)  /  public  /  javascript  
File Role Description
  Accessible without login Plain text file database.js Data JavaScript file (minified)
  Accessible without login Plain text file database.src.js Data JavaScript file
  Accessible without login HTML file index.html Conf. Prevent directory listing

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:2,087
This week:0
All time:1,877
This week:455Up
 User Ratings  
 
 All time
Utility:68%StarStarStarStar
Consistency:75%StarStarStarStar
Documentation:-
Examples:75%StarStarStarStar
Tests:-
Videos:-
Overall:49%StarStarStar
Rank:2832