0
votes
class DBconnection {

    protected $connection;

    public function __construct(){
        $this->dbConnect();
    }

    public function dbConnect() {
        try {

            // Try and connect to the database, if a connection has not been established yet{
            // Load configuration as an array. Use the actual location of your configuration file
            $config = parse_ini_file("config.ini"); 
            $dsn = 'mysql:host='. $config['host'] .';dbname=' . $config['dbname'] . ';charset=utf8;';
            $this->connection = new PDO($dsn, $config['username'], $config['password']);

            //echo 'Connection to database has been successfull!';
            return $this->connection;

        } catch(Exception $e) {
            echo 'There was an error while connecting to the database!';
            $this->connection = NULL;
        }
    }

    public function disConnect(){
        $this->connection = NULL;
        //echo '<br>Database connection has been disconnected successfully!';
    }
}

class DBTransaction extends DBconnection {

    public function __construct(){
        parent::__construct();
    }

    // FETCH ALL DATA
    public function select_all($query) {
      try {
            $result = $this->connection->query($query);
                $result->execute();
                    return $result;

        } catch (PDOException $e) {
            throw new Exception($e->getMessage());  
        }
  }

  // FETCH SPECIFIC DATA
  public function select_specific($query, $params = []) {
    try {
            $result = $this->connection->prepare($query);
              $result->execute($params);
                return $result;

        } catch (PDOException $e) {
            throw new Exception($e->getMessage());  
        }
  }

  // EXECUTE INSERT, UPDATE, DELETE
    public function execute_query($query, $params = []) {
        try {
            $stmt = $this->connection->prepare($query);
                $stmt->execute($params);
                    return $stmt;   

        } catch (PDOException $e) {
            throw new Exception($e->getMessage());  
        }
    }
}

The problem is I am getting this error:

PHP Fatal error: Call to a member function prepare() on null in /home/pipedu/public_html/msgbrd/class/DBTransaction.php on line 58

Where did I made a mistake. When I run this on localhost, it just works fine.

1
Line fifty eight - Alex
your db connection just fails, so connection is null. - Jeff
please do not write another wrapper for an existing db api. - Jeff
IndbConnect the line $this->connection = NULL; is a mistake. If you can't properly handle the event of the connection failing then don't handle the exception at all and let it propagate so you can see that it failed. - apokryfos
@mickmackusa if it's indeed an issue with credentials then I could say it's something that falls under the general umbrella of "typo" like errors so probably off-topic. However there's not enough information here for us to be certain it's to do with credentials and that's also a side-effect of swallowing up the exception (and this also makes it off-topic because it's unclear) - apokryfos

1 Answers

1
votes

You set the connection property of your class to NULL in the catch block inside DBconnection::dbConnect() with $this->connection = NULL;. Yet, in your other functions you do not check for that. By not throwing an error for that you make it an accepted state within your class.

Your solution would be:

  1. Do not catch the exception from your database connection in the class, but catch it upon initializing an instance of it instead and handle it accordingly. Like this DBconnection->connection = NULL would become an illegal state in your class and you could ignore that case in your other methods.

Example:

try {
    $con = new DBTransaction();
    $result = $con->select_all('SELECT * FROM table');
} catch (Exception $e) {
    $result = NULL;
}
  1. Hande the state, that DBconnection->connection might be NULL in your other methods and return and approriate value for that case:

Example:

public function select_all($query) {
    if (this->connection === NULL) {
        return NULL;
    }

    $result = $this->connection->query($query);
    $result->execute();
    return $result;
}