What potential pitfalls should be considered when manipulating database queries in PHP?

Potential pitfalls when manipulating database queries in PHP include SQL injection attacks, which can occur when user input is not properly sanitized before being included in a query. To prevent this, always use prepared statements with parameterized queries to securely pass user input to the database. Example PHP code snippet using prepared statements to prevent SQL injection:

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

// User input
$userInput = $_POST['user_input'];

// 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', $userInput);

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

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

// Use the results as needed
foreach ($results as $row) {
    echo $row['username'] . "<br>";
}