What are the potential pitfalls of using arrays in SQL queries in PHP, and how can they be avoided?

Potential pitfalls of using arrays in SQL queries in PHP include SQL injection vulnerabilities and syntax errors. To avoid these pitfalls, it is recommended to use prepared statements with placeholders for dynamic values in the query.

// Example of using prepared statements to avoid SQL injection
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

$name = $_POST['name']; // Assuming this value is user input

$stmt = $pdo->prepare('SELECT * FROM users WHERE name = :name');
$stmt->bindParam(':name', $name);
$stmt->execute();

$results = $stmt->fetchAll();