How can one effectively debug PHP code that involves database connections and queries, especially when using external hosting providers like Funpic?

When debugging PHP code that involves database connections and queries on external hosting providers like Funpic, it is important to check for any errors in the connection settings, query syntax, and data retrieval process. One effective way to debug is to use error reporting functions like error_reporting() and mysqli_error() to identify and address any issues in the code.

<?php
// Establish database connection
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

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

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// Perform database query
$sql = "SELECT * FROM table";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // Output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
    }
} else {
    echo "0 results";
}

// Close connection
mysqli_close($conn);
?>