How can the use of mysqli_* or PDO in PHP improve database security and prevent SQL injections when working with user favorites?

Using mysqli_* or PDO in PHP can improve database security and prevent SQL injections when working with user favorites by allowing for the use of prepared statements. Prepared statements separate SQL code from user input, preventing malicious SQL queries from being executed. This helps to protect the database from potential attacks.

// Using mysqli_* to prevent SQL injections when working with user favorites
$mysqli = new mysqli("localhost", "username", "password", "database");

// Assuming $userId and $favoriteId are user inputs
$stmt = $mysqli->prepare("INSERT INTO user_favorites (user_id, favorite_id) VALUES (?, ?)");
$stmt->bind_param("ii", $userId, $favoriteId);
$stmt->execute();
$stmt->close();

$mysqli->close();