PHP Classes

File: etc/Database.php

Recommend this page to a friend!
  Classes of Adrian M   upMVC   etc/Database.php   Download  
File: etc/Database.php
Role: Class source
Content type: text/plain
Description: Class source
Class: upMVC
Pure PHP web development without other frameworks
Author: By
Last change: upMVC
Important code update.

Beta2.0 - upMVC - Important code update.
Date: 2 months ago
Size: 974 bytes
 

Contents

Class file image Download
<?php


namespace upMVC;

use
PDO;
use
PDOException;

class
Database
{
    private
$host;
    private
$databaseName;
    private
$username;
    private
$password;
    public
$conn;

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

    private function
loadConfig()
    {
       
$this->host = ConfigDatabase::get('db.host');
       
$this->databaseName = ConfigDatabase::get('db.name');
       
$this->username = ConfigDatabase::get('db.user');
       
$this->password = ConfigDatabase::get('db.pass');
    }

    public function
getConnection()
    {
       
$this->conn = null;
        try {
           
$this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->databaseName, $this->username, $this->password);
           
$this->conn->exec("set names utf8");
        } catch (
PDOException $exception) {
            echo
"Database could not be connected: " . $exception->getMessage();
        }
        return
$this->conn;
    }
}