What are the best practices for handling errors and warnings related to mysqli functions in PHP?

When handling errors and warnings related to mysqli functions in PHP, it is important to check for errors after each mysqli function call and handle them appropriately. This can be done by using the mysqli_error() function to retrieve the error message and mysqli_errno() to retrieve the error number. Additionally, using error reporting functions like error_reporting() and ini_set('display_errors', 'On') can help in identifying and troubleshooting issues.

// Example of handling errors and warnings related to mysqli functions
$mysqli = new mysqli("localhost", "username", "password", "database");

if ($mysqli->connect_errno) {
    die("Failed to connect to MySQL: " . $mysqli->connect_error);
}

$result = $mysqli->query("SELECT * FROM table");

if (!$result) {
    die("Error in query: " . $mysqli->error);
}

while ($row = $result->fetch_assoc()) {
    // Process the data
}

$mysqli->close();