Skip to content
This repository has been archived by the owner. It is now read-only.
PHP class that use XML file like FlatFileDatabase
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Follow new project Lazer Database which evolve from XML-FileDatabase

File Database based on XML

PHP Class to use XML file(s) like a FlatFileDatabase


  • PHP 5 +
  • SimpleXML
  • DOMXPath

XML file database schema

Example of table news with few records.


<?xml version="1.0" encoding="UTF-8"?>
<table name="news" lastID="4">
    <row id="1">
        <field name="title" type="string">My first news</field>
        <field name="author" type="string">Grego</field>
        <field name="content" type="string">
          Lorem ipsum dolor sit amet enim. Etiam ullamcorper. Suspendisse a pellentesque dui, non felis. Maecenas malesuada elit lectus felis, malesuada ultricies. 
    <row id="4">
        <field name="title" type="string">Some other news</field>
        <field name="author" type="string">Admin</field>
        <field name="content" type="string">
          Curabitur et ligula. Ut molestie a, ultricies porta urna. Vestibulum commodo volutpat a, convallis ac, laoreet enim.


First of all you should define constant DB_PATH containing absolute path to folder with XML files and include class file:

 define('DB_PATH', __DIR__.'/db/');
 require 'xmlDB.php';

I assume that our XML path looks like db/news.xml.

Create database


Remove database



Multiple select

$news = Database::factory('news')
          ->select()->order_by('id', 'DESC')->find_all();

foreach($news as $post)
  echo '<h1>'.$post->title.'</h1>';
  echo '<small>author: '.$post->author.', ID: '.$post->id.'</small>';
  echo '<p>'.$post->content.'</p>';

No need to use order_by_desc()

Single record select

$news = Database::factory('news', 1)

echo '<h1>'.$news->title.'</h1>';
echo '<small>author: '.$news->author.', ID: '.$news->id.'</small>';
echo '<p>'.$news->content.'</p>';

Type ID of record after file name in factory() method. Don't use any methods after select()

Get few fields

You haven't to load all fields from record. Possible to specify names of fields you want to get in select() method by typing them in array:

select(array('title', 'content'))

Will return only fields title and content

Chain methods for select() in multiple mode
  • find_all() - returns array of rows.

  • count() - return count of rows.

  • order_by() - sort rows by key and in order. First parameter is key second (optional) order.

  • limit() - return specified number of rows. Second parameter (optional) it's offset.

  • where() - return row(s) with specified field value - (column, operator, value).

  • or_where() - alias for where(). Other type of sorting results. Example:

    $row = Database::factory('news')->select()->where('author', '=', 'admin')->or_where('author', '=', 'me')->find_all(); //will return all news where author is Admin and Me


It's pretty simple:

$row = Database::factory('news');

$row->title = 'Inserted news';
$row->author = 'me';
$row->content = 'Some content to my inserted news';


It will add new row on the end of DOM.


It's very smilar to Inserting, you must set ID of row you want to edit in factory() second parameter.

$row = Database::factory('news',1); //Will edit row with ID 1

$row->title = 'Edited news';
$row->author = 'me';
$row->content = 'Some edited content to my inserted news';


Don't forget about to call select() method


Single record deleting

$row = Database::factory('news',1); //Will remove row with ID 1

Multiple records deleting


$row = Database::factory('news',1); //Will remove row with ID 1

Multiple records deleting

Database::factory('news')->select()->where('author', '=', 'admin')->delete();


That's all for now, I think it is possible to write something else in future ;)

My homepage:

If you like and using/want to use my repo send few words to my e-mail ;)

You can’t perform that action at this time.