0

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>"; 
    }   
}
?>

1 Answer 1

2

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>"; 
    }   
}
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.