Table of Contents
StatusDraft
TodoProof read this page, check links

Database Library

The database library provides database access to your application using drivers.

Currently we have the following drivers available:

  1. MsSQL
  2. MySQL
  3. MySQLi
  4. PostgreSQL
  5. PDOSqlite (available in SVN)

Table of Contents

Quick Examples

The following is example code for using common database functionality. For more in depth help please read the individual topics linked above.

Initializing the database

$db = new Database();
 
// or
 
$db = new Database('groupname');  // "default" is assumed if groupname is not given

Simple Query

$result = $db->query('SELECT username,password,email FROM users');
 
foreach ($result as $row)
{
    echo $row->username;
    echo $row->password;
    echo $row->email;
}

Quick Example 2

This demonstrates using the query results in a template.

Query

class Clients_Controller extends Controller {
 
  public function index()
  {
 
    $db = new Database;
    $result = $db->query('SELECT name, code FROM clients');
 
    $v = new View('clients');
    $v->result = $result;
    $v->render(TRUE);
    }
}

Template

<html>
<head>
<style>
/*
 * Zebra rows: When CSS3 is done we could simply use:
 *   tr :nth-child(odd) { background-color: #D0D0D0; }
 * but for now we use PHP and CSS
 */
 
table.db tr { background-color: #F0F0F0; }
table.db tr.odd { background-color: #D0D0D0; }
table.db th { color: #f0f0f0; background-color: #303030; }
 
</style>
</head>
<body>
 
<h2>Client List</h2>
<hr/>
 
<table class="db">
<tr>
	<th>Client</th>
	<th>ID</th>
</tr>
<?php foreach( $result as $row ):?>
<tr <?= text::alternate( '', ' class="odd"' ) ?>>
	<td><?= $row->name ?> </td>
	<td><?= $row->code ?> </td>
</tr>
<?php endforeach; ?>
</table>
 
</body>
libraries/database.txt · Last modified: 2008/11/12 05:28 by samsoir