What are some potential pitfalls when using SQL queries with PHP?

One potential pitfall when using SQL queries with PHP is the risk of SQL injection attacks if user input is not properly sanitized. To prevent this, you should always use prepared statements with parameterized queries to securely pass user input to the database.

// Example of using prepared statements with parameterized queries to prevent SQL injection

// Establish a database connection
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Prepare a SQL statement with a placeholder for the user input
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');

// Bind the user input to the placeholder
$stmt->bindParam(':username', $_POST['username']);

// Execute the query
$stmt->execute();

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