RdsDataService client

This page contains examples with the RdsDataService client. See the client introduction for a more detailed description how to use a client. You may also want to consider the authentication documentation to understand the many ways you can authenticate with AWS.

The RdsDataService package could be installed with Composer.

composer require async-aws/rds-data-service

A new client object may be instantiated by:

use AsyncAws\RdsDataService\RdsDataServiceClient; $rdsDataService = new RdsDataServiceClient();

The authentication parameters is read from the environment by default. You can also specify a AWS access id and secret:

use AsyncAws\RdsDataService\RdsDataServiceClient; $rdsDataService = new RdsDataServiceClient([ 'accessKeyId' => 'my_access_key', 'accessKeySecret' => 'my_access_secret', 'region' => 'eu-central-1', ]);

For all available options, see the configuration reference.

Usage

Execute an sql statement

use AsyncAws\RdsDataService\RdsDataServiceClient; $client = new RdsDataServiceClient(); $response = $client->executeStatement([ 'database' => 'my_database', 'resourceArn' => 'arn:resource', 'secretArn' => 'arn:secret', 'sql' => 'SELECT name FROM users WHERE id = :id', 'parameters' => [ ['name' => 'id', 'value' => ['longValue' => 5]], ] ]); foreach ($response->getRecords() as $record) { echo "name: " . $record[0]->getStringValue() . PHP_EOL; }

Run a transaction

use AsyncAws\RdsDataService\RdsDataServiceClient; $database = [ 'database' => 'my_database', 'resourceArn' => 'arn:resource', 'secretArt' => 'arn:secret', ]; $client = new RdsDataServiceClient(); $transaction = $client->beginTransaction($database); try { $result = $client->executeStatement($database + [ 'transaction' => $transaction->getTransactionId(), 'sql' => 'SELECT age FROM users WHERE id = :id FOR UPDATE', 'parameters' => [ ['name' => 'id', 'value' => ['longValue' => 5]], ] ]); $user = $result->getRecords()[0] ?? null; if ($user === null) { throw new \RuntimeException("User 5 not found."); } $newAge = $user[0]->getLongValue() + 1; $client->executeStatement($database + [ 'transaction' => $transaction->getTransactionId(), 'sql' => 'UPDATE users SET age = :new_age WHERE id = :id', 'parameters' => [ ['name' => 'id', 'value' => ['longValue' => 5]], ['name' => 'new_age', 'value' => ['longValue' => $newAge]], ] ]); $client->commitTransaction($database + [ 'transaction' => $transaction->getTransactionId(), ]); } catch (\Throwable $e) { // Make sure to allways rollback since there is no connection. // If you forget than the transaction might block tables for up to 5 minutes. $client->rollbackTransaction($database + [ 'transaction' => $transaction->getTransactionId(), ]); throw $e; }


The source code to this page is found on GitHub.