What steps should be taken to establish a connection to the server before querying a database in PHP?
Before querying a database in PHP, you need to establish a connection to the server hosting the database. This can be done using the mysqli_connect() function in PHP, which requires the server name, username, password, and database name as parameters. Once the connection is established, you can then proceed to query the database using mysqli_query().
// Establish connection to the database server
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "my_database";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}