How can the use of HTML sanitization functions like htmlspecialchars affect database operations in PHP?

When using HTML sanitization functions like htmlspecialchars in PHP, it can affect database operations by preventing SQL injection attacks. By escaping special characters in user input before inserting them into the database, it ensures that the data is treated as plain text and not executable SQL code. This helps to protect the database from malicious attacks that could compromise its integrity.

// Sanitize user input before inserting into the database
$user_input = htmlspecialchars($_POST['user_input']);

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

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

// Insert sanitized user input into the database
$sql = "INSERT INTO myTable (column_name) VALUES ('$user_input')";
$conn->query($sql);

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