PHP / MySQL dbconfig + dbconnect

// dbconfig.php <?php //Database user define ('DB_USER', 'MYSQL_USERNAME'); //Database user password define ('DB_PASSWORD', 'MYSQL_PASSWORD'); ?> // dbconnection.php <?php require_once 'db.config.php'; class dbconnect { public $user = DB_USER; public $pass = DB_PASSWORD; public $connectionString = 'mysql:host=localhost;dbname=DATABASE_NAME'; public $connection; public function __construct() { try { $this->connection = new PDO($this->connectionString, $this->user, $this->pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'')); } catch (PDOException $ex) { echo 'Error: ' . $ex->getMessage() . '<br />'; die(); } } public function close() { $this->connection = null; } } ?> // usage example $db = new dbconnect(); // PDO object is accessible from $db->connection; // close connection $db->close();

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.