What are the potential pitfalls of using MySQL queries in PHP to calculate and display user statistics based on points?

One potential pitfall of using MySQL queries in PHP to calculate and display user statistics based on points is the risk of SQL injection if user input is not properly sanitized. To solve this issue, it is important to use prepared statements or parameterized queries to prevent malicious SQL injection attacks.

// Example of using prepared statements to calculate and display user statistics based on points

// Assuming $userId is the user ID for whom we want to calculate statistics

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

// Prepare a SQL query to calculate total points for a specific user
$stmt = $pdo->prepare("SELECT SUM(points) AS total_points FROM user_points WHERE user_id = :userId");
$stmt->bindParam(':userId', $userId, PDO::PARAM_INT);
$stmt->execute();
$userStats = $stmt->fetch(PDO::FETCH_ASSOC);

// Display the total points for the user
echo "Total points for user with ID $userId: " . $userStats['total_points'];