What are the best practices for using SQL queries in PHP to manipulate and summarize data?

When using SQL queries in PHP to manipulate and summarize data, it is important to follow best practices to ensure efficient and secure code. This includes using parameterized queries to prevent SQL injection attacks, properly sanitizing user input, and utilizing appropriate SQL functions for data manipulation and aggregation.

// Example of using parameterized queries to manipulate and summarize data
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Summarize data using SQL aggregation functions
$sql = "SELECT SUM(sales_amount) AS total_sales FROM sales WHERE sale_date >= :start_date AND sale_date <= :end_date";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':start_date', $start_date);
$stmt->bindParam(':end_date', $end_date);
$stmt->execute();

$total_sales = $stmt->fetchColumn();

echo "Total sales from $start_date to $end_date: $total_sales";