What are the security implications of directly accessing database values in PHP without proper sanitization?

Directly accessing database values in PHP without proper sanitization can lead to SQL injection attacks, where malicious code can be injected into SQL queries, potentially exposing sensitive information or compromising the integrity of the database. To mitigate this risk, it is important to sanitize user input before using it in SQL queries, either by using prepared statements or by escaping special characters.

// Example of using prepared statements to sanitize input
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');
$stmt->execute(['username' => $_POST['username']]);
$user = $stmt->fetch();