Connect to MySQL database using PDO

Below is how to set up a PDO connection to a MySQL database.

<?php
 
define('DATABASE','db_name');
define('DATABASE_USER','db_user');
define('DATABASE_PASSWORD','db_password');
 
try {
  $db = new PDO('mysql:host=localhost;dbname='.DATABASE,
DATABASE_USER, DATABASE_PASSWORD,array(
    PDO::ATTR_PERSISTENT => true
  ));
} catch (PDOException $e) {
  print "Error!: " . $e->getMessage() . "<br/>";
  die();
}
 
?>

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
If you have questions about anything on this site, you can find me on Google+: