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.

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

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


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 examplemysqldb.
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);
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.

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);
?>

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://orhttps://. - 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.
