What are best practices for incorporating multiple criteria, such as gender and race, into SQL queries in PHP for ranking systems?

When incorporating multiple criteria such as gender and race into SQL queries in PHP for ranking systems, it is important to use proper SQL syntax and parameterized queries to prevent SQL injection. One way to achieve this is by using conditional statements in the SQL query to filter the results based on the specified criteria. Additionally, you can use placeholders in the query to dynamically insert the values of the criteria.

// Assuming $gender and $race are variables containing the specified criteria

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

// Prepare the SQL query with placeholders for gender and race
$stmt = $pdo->prepare("SELECT * FROM users WHERE gender = :gender AND race = :race ORDER BY ranking DESC");

// Bind the values of gender and race to the placeholders
$stmt->bindParam(':gender', $gender);
$stmt->bindParam(':race', $race);

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

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

// Loop through the results and do something with them
foreach ($results as $result) {
    // Do something with the data
}