2

I am studying how to write php code of DynamoDB now.

I have encounter some problems.

1) How can I know when connect to db fail

$aws = Aws::factory('config.php');
$client = $aws->get('DynamoDb');

Return client is a object, but i don't know how to check connect success or fail in my php code.

2) Same question when i try to putItem, updateItem and deleteItem

$result = $client->putItem(array(
        ...
    ));

I read the AWS SDK of PHP document, but I can't find solution.

These function return value is a array(?) and no attribute is mean success or fail.

Only queryItem() can check by 'Count' attribute.

How should I do in my php code to check these ?

Thanks in advance.

1 Answer 1

2

Regarding #1, the "connection" should not be treated the same way as a connection to a MySQL database. Requests to DynamoDB are made over HTTP(S), and this does not require that you establish an upfront connection. When you create the client object, you are not making a connection to DynamoDB, you are just configuring an HTTP client that will make requests to DynamoDB.

Regarding #2, I think you should read the SDK's Getting Started Guide, especially the sections on Working with modeled responses and Detecting and handling errors. Basically, if a request succeeds you get a Guzzle\Service\Resource\Model object, which behaves like an array (i.e., implements PHP's ArrayAccess and Traversable interfaces). If the request fails, an exception is thrown. For DynamoDB, that exception will be Aws\DynamoDb\Exception\DynamoDbException.

try {
    $result = $client->putItem(array(
        'Table' => 'my-table',
        // ...
    ));
} catch (DynamoDbException $e) {
    // The PutItem operation failed.
    echo $e->getMessage();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.