How can you establish a connection to a database in PHP using Xampp?

To establish a connection to a database in PHP using Xampp, you need to use the mysqli_connect() function. This function takes four parameters: the server name (usually "localhost"), the username, the password, and the database name. Once the connection is established, you can perform various database operations like querying, inserting, updating, and deleting data.

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "your_database_name";

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

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>