-
-
Save kartagis/eab4174f2e730054c7d2d4a83f46e1cb to your computer and use it in GitHub Desktop.
how to make a new mysql connection with PDO.
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 | |
$host = "db hostname"; | |
$dbname = "db name"; | |
$user = "db username"; | |
$pass = "db password"; | |
$charset = "UTF8MB4"; // if your db does not use CHARSET=UTF8MB4, you should probably be fixing that | |
$dsn = "mysql:host={$host};dbname={$dbname};charset={$charset}"; | |
$options = [ | |
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // highly recommended | |
PDO::ATTR_EMULATE_PREPARES => false // ALWAYS! ALWAYS! ALWAYS! | |
]; | |
try { | |
$pdo = new PDO( $dsn, $user, $pass, $options ); | |
// now $pdo is ready for use! | |
} catch ( PDOException $e ) { | |
// always catch PDOExceptions. | |
// If there's a problem here, the error message will probably contain your DB password. | |
// log the error. | |
// during development, if your server is not public, you can display the message instead if you prefer. | |
error_log( $e->getMessage() ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment