# Connecting with PHP

This guide explains how to establish a connection between a PHP application and a MySQL database using the <span class="s3">mysqli</span> extension. It walks through the necessary setup, configuration, and execution of a simple SQL query.

## **Variables**

Certain parameters must be provided to establish a successful connection to a MySQL database. Below is a breakdown of each required variable, its purpose, and where to find it. Here’s what each variable represents:

<table border="1" id="bkmrk-variable-description" style="width: 100%; border-collapse: collapse; border-color: rgb(0, 0, 0);"><thead><tr><th style="width: 9.53403%; border-color: rgb(0, 0, 0);">**Variable**

</th><th style="width: 40.8831%; border-color: rgb(0, 0, 0);">**Description**

</th><th style="width: 49.5828%; border-color: rgb(0, 0, 0);">**Purpose**

</th></tr></thead><tbody><tr><td style="width: 9.53403%; border-color: rgb(0, 0, 0);">USER

</td><td style="width: 40.8831%; border-color: rgb(0, 0, 0);">MySQL username, from the Elestio service overview page

</td><td style="width: 49.5828%; border-color: rgb(0, 0, 0);">Identifies the database user who has permission to access the MySQL database.

</td></tr><tr><td style="width: 9.53403%; border-color: rgb(0, 0, 0);">PASSWORD

</td><td style="width: 40.8831%; border-color: rgb(0, 0, 0);">MySQL password, from the Elestio service overview page

</td><td style="width: 49.5828%; border-color: rgb(0, 0, 0);">The authentication key is required for the specified USER to access the database.

</td></tr><tr><td style="width: 9.53403%; border-color: rgb(0, 0, 0);">HOST

</td><td style="width: 40.8831%; border-color: rgb(0, 0, 0);">Hostname for MySQL connection, from the Elestio service overview page

</td><td style="width: 49.5828%; border-color: rgb(0, 0, 0);">The address of the server hosting the MySQL database.

</td></tr><tr><td style="width: 9.53403%; border-color: rgb(0, 0, 0);">PORT

</td><td style="width: 40.8831%; border-color: rgb(0, 0, 0);">Port for MySQL connection, from the Elestio service overview page

</td><td style="width: 49.5828%; border-color: rgb(0, 0, 0);">The network port used to connect to MySQL. The default port is 3306.

</td></tr><tr><td style="width: 9.53403%; border-color: rgb(0, 0, 0);">DATABASE

</td><td style="width: 40.8831%; border-color: rgb(0, 0, 0);">Database Name for MySQL connection, from the Elestio service overview page

</td><td style="width: 49.5828%; border-color: rgb(0, 0, 0);">The name of the database being accessed. A MySQL instance can contain multiple databases.

</td></tr></tbody></table>

These values can usually be found in the Elestio service overview details as shown in the image below, make sure to take a copy of these details and add it to the code moving ahead.

[![image.png](https://docs.elest.io/uploads/images/gallery/2025-04/scaled-1680-/Ukwimage.png)](https://docs.elest.io/uploads/images/gallery/2025-04/Ukwimage.png)

## **Prerequisites**

- **Install PHP**
    - Check if PHP is installed by running: `php -v`
    - If not installed, download it from [php.net](https://www.php.net/downloads) and install.
    - Make sure the <span class="s1">mysqli</span> extension is enabled in your <span class="s1">php.ini</span> configuration.

### **Code**

Once all prerequisites are set up, create a new file named `<span class="s1">mysql_connect.php</span>` and add the following code:

```php
<?php
$host = "HOST";
$user = "USER";
$password = "PASSWORD";
$database = "DATABASE";
$port = PORT;

// Create connection
$conn = new mysqli($host, $user, $password, $database, $port);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected to MySQL<br>";

// Run a test query to check the MySQL version
$result = $conn->query("SELECT VERSION()");

if ($result) {
    $row = $result->fetch_assoc();
    echo "MySQL Version: " . $row["VERSION()"];
    $result->free();
} else {
    echo "Query execution failed: " . $conn->error;
}

// Close connection
$conn->close();
?>
```

To execute the script, run the PHP server in the directory where `<span class="s1">mysql_connect.php</span>` is located using:

```
php -S localhost:8000
```

Then, open a browser and go to:

```
http://localhost:8000/mysql_connect.php
```

If the connection is successful, the browser will display output similar to:

```
Connected to MySQL
MySQL Version: 8.0.36
```