How can PHP be used to query user permissions for accessing specific database tables?

To query user permissions for accessing specific database tables in PHP, you can create a separate table in your database to store user permissions for each table. Then, you can query this permissions table based on the logged-in user's ID and the table they are trying to access to determine if they have the necessary permissions.

// Assuming you have a database connection established

// Function to check user permissions for accessing a specific table
function checkUserPermissions($userId, $tableName) {
    $query = "SELECT * FROM user_permissions WHERE user_id = $userId AND table_name = '$tableName'";
    $result = mysqli_query($connection, $query);
    
    if(mysqli_num_rows($result) > 0) {
        return true; // User has permission to access the table
    } else {
        return false; // User does not have permission to access the table
    }
}

// Example of how to use the function
$userId = 1; // Logged-in user's ID
$tableName = 'products'; // Table user is trying to access

if(checkUserPermissions($userId, $tableName)) {
    echo "User has permission to access $tableName";
} else {
    echo "User does not have permission to access $tableName";
}