PHP Connection

PHP Connection to MySQL, MariaDB and Percona

Connect a PHP application hosted on the platform to MySQL, MariaDB, or Percona. The documented workflow uses the built-in MySQLi extension to establish the connection, switch databases, execute SQL queries, and close the connection.

Environment Creation

Create an environment with MySQL or MariaDB from the SQL section of the topology wizard. The source example also adds an Apache PHP application server.

Create PHP MySQL environment
Create a PHP environment with MySQL or MariaDB.

After creation, the platform emails the database administration URL and connection credentials.

MySQL connection email
The platform email contains database access and connection details.

Open the database node in a browser, sign in, go to the Databases tab, and create a database such as mysqldb.

Open MySQL in browser
Open the database administration panel.
Create MySQL database
Create the database that will be used by the PHP application.

Connection to Database

Use the built-in MySQLi extension:

mysqli_connect('{host}', '{user}', '{password}', '{db_name}');
  • {host}: database node link without the protocol part.
  • {user} and {password}: database credentials. For production, use a dedicated account with the required permissions.
  • {db_name}: target database name, for example mysqldb.

To select another database on the same server:

mysqli_select_db($connect, '{db_name}');

Close the connection when it is no longer needed:

mysqli_close($connect);
Note: Any PHP page that needs database access must include the required connection logic, directly or through a shared include file.

Checking Connection

<?php
$link = mysqli_connect('{host}', '{user}', '{password}', '{db_name}');

if (!$link) {
    die('Could not connect: ' . mysqli_connect_error());
}

echo 'Connected successfully';

mysqli_close($link);
?>

Replace the placeholders with the actual connection values. If the connection is valid, the page displays a successful connection message.

Database connected successfully
Successful PHP connection to the database.

Executing a Simple Request

The source example connects to the server, switches to the default mysql database, reads rows from the help_topic table, and displays selected values in an HTML table.

<?php
$link = mysqli_connect('{host}', '{user}', '{password}', '{db_name}');

if (!$link) {
    echo "<h2>MySQL Error!</h2>";
    exit;
}

$db = "mysql";
mysqli_select_db($link, $db);

echo '<table border="1" width="100%" bgcolor="#FFFFE1">';
echo "<tr><td>Value1</td><td>Value2</td><td>Value3</td></tr>";

$q = mysqli_query($link, "SELECT * FROM help_topic;");

for ($c = 0; $c < mysqli_num_rows($q); $c++) {
    $f = mysqli_fetch_array($q);
    echo "<tr>";
    echo "<td>$f[0]</td><td>$f[1]</td><td>$f[5]</td>";
    echo "</tr>";
}

echo "</table>";
mysqli_close($link);
?>
Database connection example application
The example query displays records from the MySQL help_topic table.

Expected Result

The PHP application connects successfully to MySQL, MariaDB, or Percona, can select the required database, execute SQL queries, display returned data, and close the connection correctly.

Important Notes

  • Use the database node hostname without http:// or https://.
  • Use a dedicated database account for production applications.
  • Replace all placeholders before testing.
  • The documented workflow uses MySQLi.
  • Close database connections after use.

Useful to Know

  • Configure primary-secondary or multi-primary replication for higher availability.
  • Schedule database backups to protect application data.
  • Use remote access when connecting from a desktop database client.
  • Use dump import/export for manual backup and restoration.