Here we developed two of our own exception objects to
make the error handling more structured.
include_once("PGSQLConnectionException.class.php");
include_once("PGSQLQueryException.class.php");
error_reporting(0);
class DAL
{
public $connection;
public $result;
public function connect($ConnectionString)
{
$this->connection = pg_connect($ConnectionString);
if ($this->connection==false)
{
throw new PGSQLConnectionException($this->connection);
}
}
public function execute($query)
{
$this->result = pg_query($this->connection,$query);
if (!is_resource($this->result))
{
throw new PGSQLQueryException($this->connection);
}
//else do the necessary works
}
}
$db = new DAL();
try{
$db->connect("dbname=golpo user=postgres2");
try{
$db->execute("select * from abc");
}
catch (Exception $queryexception)
{
echo $queryexception->getMessage();
}
}
catch(Exception $connectionexception)
{
echo $connectionexception->getMessage();
}
?>
More OOP
[ 52 ]
Now, if the code cannot connect to DB, it catches the error and displays that
Sorry, couldn't connect to PostgreSQL server: message. If the connection is
successful but the problem is in the query, it will display the proper information.
Pages:
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73