Last active
August 29, 2015 14:03
-
-
Save bencentra/6630e0ffdee885e4b1bf to your computer and use it in GitHub Desktop.
Sample file for storing database credentials and initializing a global PDO object for database operations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$dbName = ""; // Name of the database to use | |
$dbUser = ""; // Username of the database user | |
$dbPass = ""; // Password of the database user | |
$dbHost = ""; // Host of the database | |
try { | |
// Create a global PDO object | |
$pdo = new PDO( | |
// Use MySQL database driver | |
"mysql:host=$dbHost;dbname=$dbName", | |
$dbUser, | |
$dbPass, | |
// Set some options | |
array( | |
// Return rows found, not changed, during inserts/updates | |
PDO::MYSQL_ATTR_FOUND_ROWS => true, | |
// Let the database driver handle prepared statements | |
PDO::ATTR_EMULATE_PREPARES => false, | |
// Have errors get reported as exceptions, easier to catch | |
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, | |
// Return associative arrays, good for JSON encoding | |
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC | |
) | |
); | |
} | |
catch (PDOException $e) { | |
// If we can't connect the app probably is useless, so just die() | |
die("Database Connection Failed: ".$e->getMessage()); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment