What are the best practices for handling database queries in PHP when using switch case statements?

When handling database queries in PHP using switch case statements, it is important to properly sanitize user input to prevent SQL injection attacks. One way to do this is by using prepared statements with parameterized queries. This helps to separate the SQL query logic from the user input, ensuring that the input is treated as data rather than executable code.

// Example of handling database queries in PHP using switch case statements with prepared statements

// Assume $db is your database connection

// Sanitize user input
$option = isset($_GET['option']) ? $_GET['option'] : '';

// Switch case statement to handle different query options
switch ($option) {
    case 'get_users':
        $stmt = $db->prepare("SELECT * FROM users");
        $stmt->execute();
        $users = $stmt->fetchAll();
        // Process $users data
        break;
    case 'get_posts':
        $stmt = $db->prepare("SELECT * FROM posts");
        $stmt->execute();
        $posts = $stmt->fetchAll();
        // Process $posts data
        break;
    default:
        // Handle default case
        break;
}