What is Apache and how does it relate to databases in PHP?

Apache is a popular open-source web server software that is commonly used to host websites and web applications. In the context of PHP, Apache is often used in conjunction with a database management system such as MySQL to serve dynamic web pages. Apache processes PHP scripts on the server side and can communicate with a database to retrieve or store data.

// Sample PHP code snippet connecting to a MySQL database using Apache

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

echo "Connected successfully";