note 114786 added to pdo.construct
| From: | Jan | Date: | Sun, 06 Apr 2014 18:51:57 +0000 |
| Subject: | note 114786 added to pdo.construct | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-199198@lists.php.net to get a copy of this message | ||
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;
}
}
?>
----
Server IP: 91.199.167.223
Probable Submitter: 62.45.102.204
----
Manual Page -- http://php.net/manual/en/pdo.construct.php
Edit -- https://master.php.net/note/edit/114786
Del: integrated -- https://master.php.net/note/delete/114786/integrated
Del: useless -- https://master.php.net/note/delete/114786/useless
Del: bad code -- https://master.php.net/note/delete/114786/bad+code
Del: spam -- https://master.php.net/note/delete/114786/spam
Del: non-english -- https://master.php.net/note/delete/114786/non-english
Del: in docs -- https://master.php.net/note/delete/114786/in+docs
Del: other reasons-- https://master.php.net/note/delete/114786
Reject -- https://master.php.net/note/reject/114786
Search -- https://master.php.net/manage/user-notes.php