note 114786 deleted from pdo.construct by tularis
| From: | tularis@php.net | Date: | Sun, 06 Apr 2014 21:52:45 +0000 |
| Subject: | note 114786 deleted from pdo.construct by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-199199@lists.php.net to get a copy of this message | ||
Note Submitter: Jan
----
To use this in a class file, I think that this is the correct form
<?php
//Connection Class (MYSQL)
class Connection{
protected $db;
//Constructor
public function Connection(){
//Connection is NULL (100% safe to fill the var).
$conn = NULL;
//Try to connect to the databse using PDO.
try{
$conn = new PDO("mysql:host=localhost;dbname=<DB_NAME>",
"<DB_USER>", "<DB_PASS>");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//Catch errors using the 'catch' function.
} catch(PDOException $e){
echo 'ERROR: ' . $e->getMessage();
}
//Parsing the connection into the 'db' var.
$this->db = $conn;
}
//Ask for the established database connection.
public function getConnection(){
//Returning the connection.
return $this->db;
}
}
?>