Example #1
0
 /**
  * Constructor that connects connects to a MySQL engine
  * using application constants DBHOST, DBUSER, and DBPW
  * (which are defined in inc/config.php).
  *
  * If the database doesn't exist, it is created and
  * initialised it using an
  * application function with spec
  * Initialize_myDatabase($DB,$EZ)
  * where $DB refers to the current connection object, and $EZ refers to
  * this instance of DR_easy.
  */
 public function __construct()
 {
     // CONNECT TO THE DATABASE SERVER
     $dsn = "mysql:" . DBHOST . ";dbname=" . DBNAME . ";";
     $option = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_PERSISTENT => true);
     try {
         $this->pdo = new PDO($dsn, DBUSER, DBPW, $option);
         $this->pdo->query("use " . DBNAME);
     } catch (PDOException $failure) {
         DB::throwException("Connect failed during construct");
     }
 }
Example #2
0
 /**
  * The constructor function is used to connect to a MySQl engine
  * using HOST, Name, User and Passowrd.
  * from connect.php file.
  */
 public function __construct()
 {
     // This will connect the database to the server
     $dsn = "mysql:" . HOST . ";dbname=" . DBNAME . ";";
     $option = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_PERSISTENT => true);
     // Give error if the database doesn't exist or fail to load
     try {
         $this->pdo = new PDO($dsn, USER, PASS, $option);
         $this->pdo->query("use " . DBNAME);
     } catch (PDOException $failure) {
         DB::throwException("Connect failed during construct");
     }
 }
Example #3
0
 public function __construct()
 {
     // CONNECT TO THE DATABASE SERVER
     $dsn = 'mysql:' . DATABASE_HOST . ';';
     $option = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_PERSISTENT => true);
     try {
         $this->pdo = new PDO($dsn, DATABASE_USERNAME, DATABASE_PASSWORD, $option);
     } catch (PDOException $e) {
         DB::throwException("Connect failed during construct");
     }
     try {
         $this->pdo->query("use " . DATABASE_NAME . ";");
     } catch (PDOException $e) {
         $this->pdo->query("create database " . DATABASE_NAME . "; use " . DATABASE_NAME . ";");
         $this->pdo->query(DATABASE_INIT);
     }
 }
<?php

// CONNECT TO THE DATABASE SERVER
$dsn = "mysql:" . DBHOST . ";dbname=" . DBNAME . ";";
$option = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_PERSISTENT => true);
try {
    $con = new PDO($dsn, DBUSER, DBPASS, $option);
    $con->query("use " . DBNAME);
} catch (PDOException $failure) {
    DB::throwException("Failed to connect to database");
}
try {
    $con = new PDO($dsn, DBUSER, DBPASS, $option);
    $con->exec(DBTABLES);
} catch (PDOExceptio $failure) {
    DB::throwException("Failed to create tables for database");
}