What are some common pitfalls when querying a MySQL database with PHP, especially when involving multiple conditions like dates and user inputs?

One common pitfall when querying a MySQL database with PHP, especially when involving multiple conditions like dates and user inputs, is not properly sanitizing user inputs. This can lead to SQL injection attacks. To solve this issue, always use prepared statements with parameterized queries to prevent SQL injection.

// Assuming $startDate and $endDate are user inputs for date range
$startDate = $_POST['start_date'];
$endDate = $_POST['end_date'];

// Prepare a SQL statement using prepared statements
$stmt = $pdo->prepare("SELECT * FROM table_name WHERE date_column BETWEEN :start_date AND :end_date");

// Bind parameters
$stmt->bindParam(':start_date', $startDate);
$stmt->bindParam(':end_date', $endDate);

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

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