What resources or documentation can be helpful when troubleshooting PHP code for database operations?

When troubleshooting PHP code for database operations, it can be helpful to refer to the PHP documentation for functions related to database connectivity and queries. Additionally, checking the error logs and enabling error reporting in PHP can provide valuable insights into any issues that may arise. Utilizing online forums and communities dedicated to PHP development can also offer support and solutions from experienced developers.

// Example of connecting to a MySQL database and executing a query

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

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

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

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

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

$conn->close();