A neat and easy way of setting up a PDO connection


#1

After a couple of years I have finally figure a simple way to have a constant PDO connection in order to use on my website and thought it would be nice to share it with you all.

First in I a file called db_connect.php that is located in a directory of called connect and here is the simple script:
[php]<?php
/* Setup constants for local server and remote server */
if (filter_input(INPUT_SERVER, ‘SERVER_NAME’, FILTER_SANITIZE_URL) == “localhost”) {
define(‘DATABASE_HOST’, ‘localhost’); // normally it’s called localhost:
define(‘DATABASE_NAME’, ‘name of database’);
define(‘DATABASE_USERNAME’, ‘root’); // usually it’s called root:
define(‘DATABASE_PASSWORD’, ‘your password’);
} else {
define(‘DATABASE_HOST’, ‘remote host name’);
define(‘DATABASE_NAME’, ‘remote database name’);
define(‘DATABASE_USERNAME’, ‘remote username’);
define(‘DATABASE_PASSWORD’, ‘remote password’); // Should be different than your local pw:
}[/php]

The above setups up constants that can be used throughout the website page files and you would put this in a utilities.inc.php or config.php file. I happen to put it in a utilities.inc.php file:

[php]<?php
include ‘connect/db_connect.php’;
// Autoload classes from “classes” directory:
function class_loader($class) {
require(“lib/classes/” . $class . “.php”);
}

spl_autoload_register(“class_loader”);
$db = Database::getInstance();
$pdo = $db->getConnection();[/php]
Along with a class autoloader, so I can load up a class called Database that is in a filed called Database.php file, plus I set up a PDO connection ($pdo).

An finally the Database class that is in Database.php :

[php]<?php

PDO database: only one connection is allowed.

class Database {

private $_connection;
// Store the single instance.
private static $_instance;

// Get an instance of the Database.
// @return Database:
public static function getInstance() {
if (!self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}

// Constructor - Build the PDO Connection:
public function __construct() {
$db_options = array(
/* important! use actual prepared statements (default: emulate prepared statements) /
PDO::ATTR_EMULATE_PREPARES => false
/
throw exceptions on errors (default: stay silent) /
, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
/
fetch associative arrays (default: mixed arrays) */
, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$this->_connection = new PDO(‘mysql:host=’ . DATABASE_HOST . ‘;dbname=’ . DATABASE_NAME . ‘;charset=utf8’, DATABASE_USERNAME, DATABASE_PASSWORD, $db_options);
}

// Empty clone magic method to prevent duplication:
private function __clone() {

}

// Get the PDO connection:
public function getConnection() {
return $this->_connection;
}

}
[/php]

The nice thing about this is you don’t need to know OOP in order to use the pdo connection string ($pdo). I’m pretty positive that it can also be converted over to mysqli, as a matter of fact I know it is for I converted this script over to PDO myself. I hope this helps out someone who is learning PDO, for this should free up having the trouble in making the connection and concentrating on Creating, Reading, Updating and Deleting (Crud) in PHP using PDO. :wink:


#2

Strider? What do you mean by a “constant PDO connection”??? Do not understand what that comment
means. Curious…


#3

I used the wrong wording, probably should had said not having to worry about setting up a connection when going from local to remote server, plus ease of access to the connection.


#4

Okay, thanks! I thought that was what you meant. But, thought there might be something I missed.

CYA in the BITSTREAM!