Database Examples for ProtoStack

ProtoStack comes with its very own flat file database engine built in (we use PHD - see base of this page). This page will show you some examples of how to work with data and include it in your new projects.

You can see full instructions of how to create and connect to your database tables in db/howto.md. The page you are reading doesn't deal with connecting to the database, but instead shows some basic operations, such as creating tables, inserting data, and displaying table data via the template engine.

Firstly, let's create a new table.

# in engine/db-example.php

    $db->table('team', array(
    'name' => 'string',
    'age' => 'int',
    'email' => 'string'
     ))->create();

Now, let's add two rows of data.

# in engine/db-example.php

# Add an item to the table
        $db->table('team')->put(array(
         'username' => 'Bob',
         'password' => 'mary123',
         'email' => 'bobross@reddit.com'
        ));

# Add another item to the table
        $db->table('team')->put(array(
         'username' => 'Steve',
         'password' => 'ilovecheese',
         'email' => 'steve@reddit.com'
        ));

Now, 'get' the data from the 'team' table, and put it into a PHP array called $team.

# in engine/db-example.php

$team = $db->table('team')->get();

The $team PHP variable now contains the two rows of data pulled from the database using the 'get' command above. This means that we can now use it in PHP code, or simply use the template engine 'LOOP' command to manipulate the data. See below.

Now, let's use the template engine to show the data

# in templates/db-example.tpl

 <table>
  <tr>
  <th>Username</th><th>Password</th><th>Email</th>
  </tr>
  {LOOP:team}
  <tr>
  <td>{username}</td><td>{password}</td><td>{email}</td>
  </tr>
  {ENDLOOP}
</table>

The loop code above produces the table below

UsernamePasswordEmail
Bobmary123bobross@reddit.com
Steveilovecheesesteve@reddit.com

You can read more about PHD here.


© 2025 ProtoStack