Problem with Custom Error Handler
Please refer my previous post here. I did changes accordingly but getting error. Please help me figure out the errors in this code. My IDE (Aptana) is giving red underlines on many lines of this code:
<?php
/* Include dependency */
require_once("./Config/dbconfig.php");
abstract class dbconnection
{
var $conn;
try
{
//Opens connection for a MySQL DB
public function OpenConnection()
{
$conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
or die (throw new DB_Exception_Handler('Cannot connect to DB: ' . $thisException->getMessage() . '.'));
mysql_select_db(DB_NAME) or
die (throw new DB_Exception_Handler('Cannot connect to DB: ' . $thisException->getMessage() . '.')开发者_运维知识库);
}
//Closes connection for a MySQL DB
public function CloseConnection()
{
mysql_close($conn);
}
}
catch(DB_Exception_Handler($thisException)
{
$thisException->ShowError();
}
}
class DB_Exception_Handler extends Exception
{
public function ShowError()
{
echo "<script>alert('". $this->getMessage() ."');</script>";
}
}
?>
Things I notice is a try catch block within your class but not inside a method. and throwing a new exception from within a function call which is expecting either a string or an int (die()).
If you use the @ symbol you surpress error messages, both database functions return false if they fail.
Also you are calling a function on a reference $thisException that doesn't exist in the current scope it seems. But this might be because not all your code is here.
<?php
/* Include dependency */
require_once("./Config/dbconfig.php");
abstract class dbconnection {
var $conn;
//Opens connection for a MySQL DB
public function OpenConnection() {
$conn = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$conn) {
throw new DB_Exception_Handler('Cannot connect to DB: ' . mysql_error() . '.');
}
if(mysql_select_db(DB_NAME) == false) {
throw new DB_Exception_Handler('Cannot connect to DB: ' . mysql_error() . '.');
}
}
//Closes connection for a MySQL DB
public function CloseConnection()
{
mysql_close($conn);
}
}
class DB_Exception_Handler extends Exception
{
public function ShowError()
{
echo "<script>alert('". $this->getMessage() ."');</script>";
}
}
?>
精彩评论