• Stars
    star
    114
  • Rank 308,031 (Top 7 %)
  • Language
    PHP
  • License
    Other
  • Created over 11 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A compact, lightweight and feature-rich PHP MySQLi database wrapper

zebrajs

Zebra Database  Tweet

A compact, lightweight yet feature-rich PHP MySQLi database wrapper providing methods for interacting with MySQL databases that are more secure, powerful and intuitive than PHP's default ones.

Latest Stable Version Total Downloads Monthly Downloads Daily Downloads License

Zebra_Database supports transactions and provides ways for caching query results either by saving cached data to the disk, in the session, by using memcache or redis.

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

Can provides debugging information when called from CLI (command-line interface) and supports logging queries done through AJAX requests.

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.

The 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 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; this is not a wrapper for the PDO extension which is already a wrapper in itself!

  • can provide debugging information even when called from CLI (command-line interface)

  • logs all queries, including those run through AJAX

  • offers lots of powerful methods for easier interaction with MySQL

  • supports unbuffered queries

  • 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 EXPLAINs each SELECT query

  • supports caching of query results to the disk, in the session, or to a memcache or to a redis server

  • has really good documentation

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

Zebra_Database's debugging console

📔 Documentation

Check out the awesome documentation!

🎂 Support the development of this project

Your support is greatly appreciated and it keeps me motivated continue working on open source projects. If you enjoy this project please star it by clicking on the star button at the top of the page. If you're feeling generous, you can also buy me a coffee through PayPal or become a sponsor. Thank you for your support! 🎉

Star it on GitHub Donate

Requirements

PHP 5.4.0+ 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]

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

Installation

You can install Zebra Database via Composer

# get the latest stable release
composer require stefangabos/zebra_database

# get the latest commit
composer require stefangabos/zebra_database:dev-master

Or you can install it manually by downloading the latest version, unpacking it, and then including it in your project

require_once 'path/to/Zebra_Database.php';

How to use

Connecting to a database
// instantiate the library
$db = new Zebra_Database();

// connect to a server and select a database
$db->connect('host', 'username', 'password', 'database');
Running queries
// question marks will re replaced automatically with the escaped values from the array
// I ENCOURAGE YOU TO WRITE YOUR QUERIES IN A READABLE FORMAT, LIKE BELOW
$db->query('
    SELECT
    	column1
        , column2
        , column3
    FROM
    	tablename1
    	LEFT JOIN tablename2 ON tablename1.column1 = tablename2.column1
    WHERE
    	somecriteria = ?
        AND someothercriteria = ?
', array($somevalue, $someothervalue));

// any fetch method will work with the last result so
// there's no need to explicitly pass that around

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

// you could fetch all records to one associative array
// using the values in a specific column as keys
$records = $db->fetch_assoc_all('column1');

// or fetch records one by one, as associative arrays
while ($row = $db->fetch_assoc()) {
    // do stuff
}
An INSERT statement
// notice that you can use MySQL functions in values
$db->insert(
    'tablename',
    array(
        'column1'      => $value1,
        'column2'      => $value2,
        'date_updated' => 'NOW()'
    )
);
An UPDATE statement
// $criteria will be escaped and enclosed in grave accents, and will
// replace the corresponding ? (question mark) automatically
// also, notice that you can use MySQL functions in values
// when using MySQL functions, the value will be used as it is without being escaped!
// while this is ok when using a function without any arguments like NOW(), this may
// pose a security concern if the argument(s) come from user input.
// in this case we have to escape the value ourselves
$db->update(
    'tablename',
    array(
        'column1'      => $value1,
        'column2'      => 'TRIM(UCASE("value2"))',
        'column3'      => 'TRIM(UCASE("'' . $db->escape($value3) . "))',
        'date_updated' => 'NOW()'
    ),
    'criteria = ?',
    array($criteria)
);

There are over 40 methods and 20 properties that you can use and lots of things you can do with this library. I've prepared an awesome documentation so that you can easily get an overview of what can be done. Go ahead, check it out!

More Repositories

1

world_countries

Constantly updated lists of world countries and their associated alpha-2, alpha-3 and numeric country codes as defined by the ISO 3166 standard, available in CSV, JSON , PHP, SQL and XML formats, in multiple languages and with national flags included; also available are the ISO 3166-2 codes of provinces/ states associated with the countries
PHP
1,391
star
2

Zebra_Datepicker

A super-lightweight, highly configurable, cross-browser date time picker jQuery plugin
JavaScript
401
star
3

Zebra_cURL

A high-performance solution for making HTTP requests from your PHP projects. It allows running of multiple requests concurrently, asynchronously, supports GET, POST, HEADER, PUT, PATCH, and DELETE requests, and offers support for caching, FTP downloads, HTTP authentication and proxy requests.
PHP
211
star
4

Zebra_Session

A drop-in replacement for PHP's default session handler which stores session data in a MySQL database, providing better performance, better security and protection against session fixation and session hijacking
PHP
169
star
5

Zebra_Dialog

A small, compact, mobile-friendly and highly configurable jQuery plugin for creating gorgeous dialog boxes
JavaScript
155
star
6

Zebra_Image

A single-file lightweight PHP library designed for efficient image manipulation featuring methods for modifying images and applying filters
PHP
137
star
7

Zebra_Pagination

A generic, Twitter Bootstrap compatible, pagination library for automatically generating navigation links
PHP
113
star
8

Zebra_Form

A jQuery augmented PHP library for creating secure HTML forms and validating them easily
PHP
98
star
9

Zebra_Mptt

A PHP library providing an implementation of the modified preorder tree traversal (MPTT) algorithm
PHP
80
star
10

Zebra_Pin

A lightweight and adaptive jQuery plugin for creating sticky elements pinned to the page or to a container element
JavaScript
66
star
11

Zebra_Tooltips

A lightweight, accessible, and highly configurable jQuery plugin for creating beautiful tooltips
JavaScript
55
star
12

zebrajs

A modular, jQuery compatible, ultra light-weight JavaScript micro-library for modern browsers
JavaScript
32
star
13

Zebra_Accordion

A tiny (3KB minified, ~1.3KB gzipped), easily configurable, fully customizable, cross-browser jQuery accordion plugin
JavaScript
31
star
14

Zebra_TransForm

A tiny jQuery plugin for replacing and beautifying check boxes, radio buttons, and select boxes in IE7+
JavaScript
17
star
15

Zebra_Gomoku

A ridiculously small JavaScript gomoku AI implementation, as a jQuery plugin
JavaScript
17
star
16

Zebra_Cookie

A very lightweight JavaScript library for easy cookie management — set, read, and delete browser cookies effortlessly
JavaScript
17
star
17

Zebra_ClearInput

A tiny jQuery plugin for enhancing web forms by allowing users to easily clear values in text input boxes
JavaScript
5
star
18

Zebra_Cache

A file-based lightweight PHP caching library that uses file locking to ensure proper functionality under heavy load
PHP
1
star