PHP: MySQL Connection Guide

Learn how to connect to MySQL from PHP using MySQLi and PDO

👋 Welcome to the Stackhero documentation!

Stackhero offers managed PHP hosting designed for fast and reliable deployments:

  • Deploy your PHP application to production in seconds with a simple git push.
  • Use your own domain, with automatic HTTPS certificates configured for you. No manual steps required.
  • Take advantage of automatic backups, one-click updates, and clear, predictable pricing. Stackhero takes care of these details so you can focus on your code.
  • Enjoy strong performance and security on a private, dedicated infrastructure built specifically for your applications.

Streamline your workflow and save time. Getting started with Stackhero's PHP hosting for Laravel and Symfony only takes a few minutes.

The following examples show how to connect an application hosted on managed PHP hosting to a managed MySQL database, using the standard MySQLi and PDO drivers.

The example below demonstrates how to connect PHP to MySQL using MySQLi in an object-oriented style. It is recommended to create a dedicated database and user for your application, rather than using the 'root' account. This practice enhances your application's security.

<?php

$hostname = '<XXXXXX>.stackhero-network.com';
$user = 'root';
$password = '<yourPassword>';
$database = 'root'; // In production, create a dedicated database and user in phpMyAdmin

$mysqli = mysqli_init();
$mysqliConnected = $mysqli->real_connect($hostname, $user, $password, $database, NULL, NULL, MYSQLI_CLIENT_SSL);
if (!$mysqliConnected) {
  die('Connection error: ' . $mysqli->connect_error);
}

echo 'Connection successful... ' . $mysqli->host_info . "\n";

$mysqli->close();

?>

If you prefer a procedural approach, the following example shows how to establish a connection using MySQLi in procedural style. As always, it is advisable to use a dedicated database and a restricted user to strengthen security.

<?php

$hostname = '<XXXXXX>.stackhero-network.com';
$user = 'root';
$password = '<yourPassword>';
$database = 'root'; // For enhanced security, create a dedicated database and user in phpMyAdmin

$mysqli = mysqli_init();
$mysqliConnected = mysqli_real_connect($mysqli, $hostname, $user, $password, $database, NULL, NULL, MYSQLI_CLIENT_SSL);
if (!$mysqliConnected) {
  die('Connection error: ' . mysqli_connect_error($mysqli));
}

echo 'Success: ' . mysqli_get_host_info($mysqli) . "\n";

mysqli_close($mysqli);

?>

PDO provides a flexible way to connect PHP to MySQL. The example below shows how to use PDO to establish a connection. It also covers potential SSL certificate issues that may occur if your system does not have access to CA certificates.

<?php

$hostname = '<XXXXXX>.stackhero-network.com';
$user = 'root';
$password = '<yourPassword>';
$database = 'root'; // In production environments, consider creating a dedicated database and user

$dsn = "mysql:host=$hostname;dbname=$database";

$options = array(
  // If you encounter an error like "Uncaught PDOException: PDO::__construct(): SSL operation failed with code 1...",
  // it may be because the /etc/ssl/certs/ directory is missing CA certificates
  PDO::MYSQL_ATTR_SSL_CAPATH => '/etc/ssl/certs/',
  // PDO::MYSQL_ATTR_SSL_CA => 'isrgrootx1.pem',
  PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => true,
);

$pdo = new PDO($dsn, $user, $password, $options);

$stm = $pdo->query('SELECT VERSION()');
$version = $stm->fetch();

echo 'You are connected to a database running version ' . $version[0] . "\n";

?>

If you see an error such as Uncaught PDOException: PDO::__construct(): SSL operation failed with code 1. OpenSSL Error messages: error:0A000086:SSL routines::certificate verify failed, it is likely that your system's /etc/ssl/certs/ directory does not contain the required CA certificates.

If you have access to the system running your PHP code, you can install the certificates as follows:

  1. On Ubuntu/Debian, run sudo apt-get install ca-certificates
  2. On Alpine Linux, run apk add ca-certificates

If you do not have the ability to install these certificates directly on your system, you can install the certificate manually:

  1. Download the certificate to your computer: https://letsencrypt.org/certs/isrgrootx1.pem
  2. Add the isrgrootx1.pem file to your PHP project files.
  3. Comment out the line PDO::MYSQL_ATTR_SSL_CAPATH => '/etc/ssl/certs/'
  4. Uncomment the line PDO::MYSQL_ATTR_SSL_CA => 'isrgrootx1.pem'

To enhance security and simplify configuration management, it is recommended not to hardcode your credentials in your source code. Instead, retrieve these settings from environment variables as shown below:

<?php

$hostname = getenv('STACKHERO_MYSQL_HOST');
$user = getenv('STACKHERO_MYSQL_USER');
$password = getenv('STACKHERO_MYSQL_PASSWORD');
$database = getenv('STACKHERO_MYSQL_USER'); // By convention, the database name matches the username

?>

Using environment variables helps keep your credentials secure and makes your configuration more easily adaptable to different environments.