PHP PDO require file [duplicate]

  pdo, php, web

Creating a website with PHP and PDO. In my dbh.inc.php, I have:

$dbh = new PDO('mysql:host=localhost;dbname=mydb;charset=UTF8;port=****', 'user', 'pwd');

I want to replace this with:

class Dbh {

private $servername = "localhost";
private $username = "user";
private $password = "pwd";
private $dbName = "mydb";
private $charset = "UTF8";
private $port = "****";

public function connect() {

$dbh = "mysql:host=".$this->servername.";dbname=".$this->dbname.";charset=".$this->charset.";port=".$this->port;
$pdo = new PDO($dbh, $this->username, $this->password);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
return $pdo;

How to make this class to replace the simple code above? In every file I have: require('includes/dbh.inc.php');

Source: Ask PHP

LEAVE A COMMENT