Sunday, July 12, 2020
therichposts@gmail.com
MysqlPDOPhp

How to connect mysql database using pdo?

connect mysql database using pdo

Views

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()
{
$dbhost=DB_SERVER;
$dbuser=DB_USERNAME;
$dbpass=DB_PASSWORD;
$dbname=DB_DATABASE;

try{
$PDO = new PDO(“mysql:host=$dbhost;dbname=$dbname”,$dbuser,$dbpass);
$PDO->exec(“set names utf8”);
$PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $PDO;
} catch(PDOException $e){
die($e->getMessage());
}
}
}
$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

Ajay Malhotra
the authorAjay Malhotra
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know Wordpress, Core php, Angularjs, Angular 2, Angular 6, Angular 7, Angular 8, Angular 9, Angular 10 Nodejs, laravel, Codeigniter, Shopify, Squarespace, Jquery, Google Map Api, Vuejs, Reactjs, Bigcommerce etc.

Leave a Reply

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