What are some best practices for checking if an SQL query has been initiated in PHP?

When working with SQL queries in PHP, it is important to check if the query has been successfully initiated to avoid errors and ensure proper execution. One way to do this is by using error handling techniques to catch any potential issues that may arise during the query execution.

// Example of checking if an SQL query has been initiated in PHP

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

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

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

// SQL query
$sql = "SELECT * FROM table_name";
$result = $conn->query($sql);

// Check if query was initiated successfully
if ($result === false) {
    echo "Error executing query: " . $conn->error;
} else {
    // Process the query results
    while($row = $result->fetch_assoc()) {
        // Do something with the data
    }
}

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