What are the potential pitfalls of not using parameterized queries and prepared statements in PHP when accessing databases?

Not using parameterized queries and prepared statements in PHP when accessing databases can leave your application vulnerable to SQL injection attacks, where malicious users can manipulate your SQL queries to access or modify your database. To prevent this, always use parameterized queries and prepared statements to securely handle user input when interacting with databases.

// Using parameterized queries and prepared statements to securely handle user input
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Prepare a SQL statement with placeholders
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username");

// Bind the parameter values to the placeholders
$stmt->bindParam(':username', $username, PDO::PARAM_STR);

// Execute the prepared statement
$stmt->execute();

// Fetch the results
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);