Mysql PDO connection using Class

mysql pdo connection using class
<?php

class DatabaseConnection
{

	private $host = 'localhost';
	private $dbname = 'Demo';
	private  $username = 'root';
	private  $password ='';

    public $con = null;

    function __construct(){

        $this->connect();
    }

    function connect(){

        try{

            $this->con = new PDO("mysql:host=$this->host;dbname=$this->dbname",$this->username, $this->password,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
            $this->con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
            

        }catch(PDOException $e){


            echo 'connection problem'.$e->getMessage();
            

        }
    }
}

$dbobj=new DatabaseConnection();
$dbobj->connect();

12 Replies to “Mysql PDO connection using Class”

Leave a Reply

Your email address will not be published. Required fields are marked *