Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* Database class: DB */
- class DB
- {
- /* PDO resource object */
- private $pdo;
- /* Constructor */
- public function __construct(string $host, string $user, string $passwd, string $schema)
- {
- $this->connect($host, $user, $passwd, $schema);
- $this->setParams();
- }
- /* Connects to the database */
- private function connect(string $host, string $user, string $passwd, string $schema)
- {
- $this->db = new \PDO('mysql:host=' . $host . ';dbname=' . $schema, $user, $host);
- }
- /* Sets some PDO params (exceptions on error and sorting order) */
- private function setParams()
- {
- $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $this->db->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
- }
- }
- /* How to use this class */
- $host = 'localhost';
- $user = 'user';
- $passwd = 'passwd';
- $schema = 'mySchema';
- $db = NULL;
- try
- {
- $db = new DB($host, $user, $passwd, $schema);
- }
- catch (PDOException $e)
- {
- echo 'PDO connection error.';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement