Search code examples
phpmysqlamazon-web-servicesamazon-dynamodbaws-php-sdk

DynamoDB; Fetch all data in a table - PHP


I found some help in how to get this done via the answer linked here. But it doesn't fit my use case. My goal is to fetch all records in a DynamoDB Table (usernames and emails). Looking through doc I have to use LastEvaluatedKey or ExclusiveStartKey to implement pagination. Any guidance will be appreciated.

Thanks


Solution

  • This query will able to get all records from DynamoDB Table.

    function scanAllData($table,$limit){
    
      $result = $this->getClientdb()->scan(array(
            'TableName' => $table,
            'Limit' => $limit,
            'Select' => 'ALL_ATTRIBUTES'                
         ),
        array('limit' => $limit),
      );
        return $result['Items'];
    }
    

    You can call this function like this. for example you have 'users' table and columns are usernames and emails

    $getobj = $this->scanAllData('users','10');
    
    foreach($getobj as $cols){
    
       echo $cols['usernames']['S'];
       echo $cols['emails']['S'];    
    
    }