How do I connect to MySQL on Aptible using PHP?

On Aptible, MySQL Databases enforce SSL, but use a self-signed certificate. Here is how you can configure PHP to connect over SSL.

Using PDO

// Assuming you have set $hostname, $username, $password, $db, and $port

$url = "mysql:host=$hostname;port=$port;dbname=$db;charset=utf8";

new PDO(
  $url, $username, $password,
  array(
    PDO::MYSQL_ATTR_SSL_CIPHER => 'DHE-RSA-AES256-SHA',
    PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false
  )
);

Using mysqli

// Assuming you have set $hostname, $username, $password, $db, and $port

$link = mysqli_init();

mysqli_real_connect(
  $link, $hostname, $username, $password, $db, $port, NULL,
  MYSQLI_CLIENT_SSL | MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT
);