Location: PHPKode > scripts > Persist Class > persist-class/demo4-errorhandling.php
<?php

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';


?>
Return current item: Persist Class