Skip to content

Instantly share code, notes, and snippets.

@innbitcodes
Created January 5, 2014 10:14
Show Gist options
  • Select an option

  • Save innbitcodes/8266526 to your computer and use it in GitHub Desktop.

Select an option

Save innbitcodes/8266526 to your computer and use it in GitHub Desktop.
Way to connect MySQL with PDO method in PHP
<?php
try{
$db = new PDO("mysql:host=HOST_NAME;dbname=DB_NAME;port=PORT_NUM", "USER", "PASS");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec("SET NAMES 'utf8'");
} catch (Exception $e) {
echo "Could not connect to database.";
exit;
}
try {
$results = $db->query("SELECT .. FROM .. WHERE ..");
} catch (Exception $e) {
echo "Something wrong!";
}
$results->fetchAll(PDO::FETCH_ASSOC);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment