@domenico
To insert multiple rows in DynamoDB using PHP, you can use the BatchWriteItem
API provided by the AWS SDK for PHP. Here is an example code snippet to insert multiple rows in DynamoDB using PHP:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<?php require 'vendor/autoload.php'; use AwsDynamoDbDynamoDbClient; // Initialize the DynamoDB client $client = DynamoDbClient::factory([ 'region' => 'us-east-1', 'version' => 'latest', ]); // Define the items to be inserted $items = [ [ 'TableName' => 'YourTableName', 'Item' => [ 'id' => ['N' => '1'], 'name' => ['S' => 'John Doe'], // Add more attributes here ], ], [ 'TableName' => 'YourTableName', 'Item' => [ 'id' => ['N' => '2'], 'name' => ['S' => 'Jane Doe'], // Add more attributes here ], ], // Add more items here ]; // Insert the items using BatchWriteItem API $result = $client->batchWriteItem([ 'RequestItems' => $items, ]); // Check for any errors if ($result->get('UnprocessedItems')) { echo "Some items were not processed. "; } else { echo "All items have been inserted successfully. "; } ?> |
In this code snippet, you need to replace 'YourTableName'
with the name of your DynamoDB table, and you can add more attributes to the items as needed. You can also add more items to the $items
array to insert multiple rows at once.
Please make sure to properly set up the AWS PHP SDK and provide the necessary credentials and configuration before running the code.