/**
* This example demonstrates the basics of using DAL
* The database used for this example is very simple
*
*
* create table users (
* id int,
* name char(20)
* );
*
* @package dal
*/
include "class_dal.phps";
/**
* First create a new object of the class DAL
*/
$foo = new dal;
/**
* Second select the driver we want to use
* (in this case the postgres driver)
*/
$foo->selectDriver("postgres");
/**
* Tell everybody which driver we are using.
*/
echo "<h1> DAL Demonstration </h1>";
echo "<p>Now using driver: " . $foo->getDriverName();
echo "</p>";
/**
* Select configuration "conf2"
*/
if (!$foo->selectConfiguration("conf2")) {
echo "<p>$foo->lastError";
}
/**
* Select database test
* If anything goes wrong echo the error message.
*/
if (!$foo->selectDatabase("dal_database")) {
echo $foo->lastError;
}
/**
* Select everything from users.
* If anything goes wrong echo the error message.
* If there are results try to fetch them as an object.
*/
echo "<p>select max(id) from users;</p>";
/**
* Ok, msql doesn't understand the max function so it
* if we are using that driver we need a more simple
* sql statement
*/
if (preg_match("/msql/i", $foo->getDriverName())) {
$query = "select id from users order by id desc";
}
else {
$query = "select max(id) as maxid from users";
}
if (!$foo->executeQuery($query)) {
echo $foo->lastError;
$i = 0;
}
else {
echo "<p>Rowcount : $foo->rowcount<br>";
$bar = $foo->fetchObject();
if ($bar == NULL) {
echo $foo->lastError;
}
else {
echo "<p>We got the object: ";
var_dump($bar);
if (preg_match("/msql/i", $foo->getDriverName())) {
$i = $bar->id;
}
else {
$i = $bar->maxid;
}
$i++;
}
}
/**
* Insert into users some values.
* If anything goes wrong echo the error message.
*/
echo "<p>insert into users values ($i, 'testuser');";
if (!$foo->executeQuery("insert into users values ($i, 'testuser')")) {
echo $foo->lastError;
}
else {
echo "<p>Rowcount: $foo->rowcount<br>";
$bar = $foo->fetchObject();
var_dump($bar);
if ($bar == NULL) {
echo "<p> \$bar seems to be null, let's see why: ";
echo $foo->lastError;
}
}
/**
* Let's select some rows and show the different fetching methods
*/
echo "<h1> Fetching stuff </h1>";
if (!$foo->executeQuery("select * from users")) {
echo $foo->lastError;
}
else {
echo "<h3> Fetching an associative array </h3>";
$bar = $foo->fetchArray();
echo "<p>";
var_dump($bar);
echo "<h3> Fetching an enumerated array </h3>";
$bar = $foo->fetchRow();
echo "<p>";
var_dump($bar);
echo "<h3> Fetching an object </h3>";
$bar = $foo->fetchObject();
echo "<p>";
var_dump($bar);
}
/**
* Hmmm... what did we last inserted to the database ?
*/
echo "<h2> Last insert on the database </h2>";
echo "<p>";
var_dump($foo->fetchLastInsert());
echo "<h2> Last autogenerated id </h2>";
echo "<p>";
var_dump($foo->fetchInsertID());
echo "<h2>custom error Handler</h2>";
/**
* Set a custom error handler
* and make sure we get an error.
*/
$foo->setErrorHandler("myErrorHandler");
$foo->executeQuery("bla;");
$foo->startTransaction();
$foo->destroy();
function myErrorHandler($errormessage)
{
echo "<p>Custom error: $errormessage";
}
|