Skip to content

How to Check MySQL Server Connection

Is there a way to check if MySQL server is connected (or not)?

To check if a MySQL server is connected when using ExeOutput for PHP, you can use PHP's MySQLi extension.

First, enable the MySQLi PHP extension so your PHP application can interact with MySQL. To do this, go to the "PHP Settings -> PHP Extensions" page in ExeOutput for PHP.

Then, you can use the following PHP code as a straightforward approach to check the connection and display an alert if the connection fails:

<?php
// Database connection parameters
$host = 'localhost'; // Your MySQL server host
$username = 'your_username'; // Your MySQL username
$password = 'your_password'; // Your MySQL password
$database = 'your_database'; // Your MySQL database name

// Create a connection to the MySQL database using MySQLi
$connection = new mysqli($host, $username, $password, $database);

// Check connection
if ($connection->connect_error) {
    // Connection failed, display alert
    echo "<script type='text/javascript'>alert('Connection to MySQL server failed: " . $connection->connect_error . "');</script>";
} else {
    // Connection successful
    echo "Connected successfully to the MySQL server.";
}

// Close the connection
$connection->close();
?>