Home Mysql How to connect mysql database using pdo?

How to connect mysql database using pdo?

by therichpost
0 comment
Php Preg_Match Pattern for Week Days Name

Hello to all, welcome to therichpost.com. In this post, I will tell you, How to connect mysql database using pdo? PDO means PHP Data Object. PDO is more secure and it is opps based.

Here is the example code for connect mysql database using pdo:

define(‘DB_SERVER’, ‘localhost’);
define(‘DB_USERNAME’, ‘root’);
define(‘DB_PASSWORD’, ‘root’);
define(‘DB_DATABASE’, ‘testdatabse’);

class Connection{

public static $PDO =null;

public static function getConnectionInstance()

$PDO = new PDO(“mysql:host=$dbhost;dbname=$dbname”,$dbuser,$dbpass);
$PDO->exec(“set names utf8”);
return $PDO;
} catch(PDOException $e){
$DBH = Connection::getConnectionInstance();

There are so many tricky code in PDO and I will let you know all. Please do comment if you any query related to this post. Thank you. Therichpost.com

You may also like

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.