Why is it recommended to avoid using SET NAMES in PHP for specifying character encoding?

Using SET NAMES in PHP for specifying character encoding is not recommended because it can lead to SQL injection vulnerabilities. Instead, it is better to set the character encoding directly in the connection string or using the mysqli_set_charset function to ensure secure and proper handling of character encoding.

$mysqli = new mysqli("localhost", "username", "password", "database");

if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

$mysqli->set_charset("utf8");