Are there specific tutorials or resources available for learning about secure database handling in PHP, especially with regard to mysqli functions?

When handling databases in PHP, it is crucial to follow best practices for security to prevent SQL injection attacks. One way to achieve this is by using mysqli functions, which offer prepared statements to safely handle user input.

// Establish a connection to the database
$mysqli = new mysqli("localhost", "username", "password", "database");

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

// Use prepared statements to safely handle user input
$stmt = $mysqli->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);

// Set parameters and execute
$username = "example_username";
$stmt->execute();

// Get the result set
$result = $stmt->get_result();

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

// Close the statement and connection
$stmt->close();
$mysqli->close();