Login   Register  
PHP Classes
elePHPant
Icontem

File: demo4-errorhandling.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Bob Gombocki  >  PersistClass  >  demo4-errorhandling.php  >  Download  
File: demo4-errorhandling.php
Role: Example script
Content type: text/plain
Description: Error handling
Class: PersistClass
DB access wrapper & storing objects in DB tables
Author: By
Last change: added web tutorial info
Date: 5 years ago
Size: 1,262 bytes
 

Contents

Class file image Download
<?php

/*    
    http://coolpenguin.net/persistclass 
    for updates, documentation, tutorials
*/

require('persistclass/PersistClass.php');

// create a database connection
try {
    
$myConnection = new DbConnectionMysql();
    
$myConnection->connect('localhost''demouser''demopassword''demodatabase');
    
DbConnectionPool::instance()->registerConnection($myConnection);
} catch(
ConnectionException $e) {
    
// problem with connection, display error message
}

// preparation for the demo (inserting a row with id = 1)
class TestTable extends PersistClass {
    protected 
$sqlTableName 'TESTTABLE';
    protected 
$sqlPrimaryKey 'testid';
}

try {
    
$someId 153;
    
$test = new TestTable($someId);
} catch(
NoResultException $e) {
    
// object doesn't exist, do something
}

try {
    
$id 1;
    
$test = new TestTable($someId);
    echo 
$test->getData('thiscolumndoesntexist');
} catch(
NoDataException $e) {
    
// referring to non-existing columns will throw NoDataException
} catch(NoResultException $e) {
    
// trying to initiate non-existing object
}

$con DbConnectionPool::instance()->getConnection();
try {
    
$con->query("This is not a valid sql query");
} catch(
QueryException $e) {
    
// query failed
}

echo 
'Test successful';


?>