user2463882
user2463882

Reputation: 53

dynamodb getitem using php - I only want to retrieve the value

I'm able to query my dynamodb tables, but I only want to retrieve the actual value. I don't want the formatting output. This same question has been answered here for Java, but I'm looking for the PHP solution: Retrieving just the item value from a dynamodb table?

Here is my getitem query:

$response = $dynamodb->getItem(array(
    "TableName" => $tableName,
    "ConsistentRead" => true,
    "Key" => array(
        "userguid" => array(Type::STRING => $userguid)
    ),
    "AttributesToGet" => array("token")
));
print_r($response["Item"]["token"]);

Here is the output:

Array
(
    [S] => 9d194513
)

All I want to get back is:

9d194513

I assumed the logical answer would be to change the last line to:

print_r($response["Item"]["token"]["S"]);

But then my code doesn't return anything at all. Obviously still learning PHP here, and any help would be appreciated.

Upvotes: 5

Views: 4881

Answers (3)

rashidkhan
rashidkhan

Reputation: 472

Though it's an old question but for anyone coming to this page for seeking answer, this is how I have done it.

getItem returns a Resultobject. You can call the get() function of the SDK, which will give you an array containing the exact value.

$params = [
        "TableName" => "EpgApiAccessCount",
        "Key" => $this->marshalJson('
            {
                "ApiUserKey": "' . $apiUserkey . '"
            }
        ')
    ];

    $result = $this->client->getitem($params);
    if (!$result instanceof ResultInterface) {
        return 0;
    }


    $item = $this->unmarshalItem($result->get("Item"));
    return $item["AccessCount"];

Of course your value and table name will be different, and you can print or do anything else with the value.

Upvotes: 1

Jeremy Lindblom
Jeremy Lindblom

Reputation: 6527

You can also use the getPath convenience method built into the Model object that the SDK returns for operations.

echo $response->getPath('Item/token/S');

For more information about working with responses in the SDK, see the Response Models page in the AWS SDK for PHP User Guide.

Upvotes: 2

Fabio
Fabio

Reputation: 23510

Don't use print_r function, just either echo your variables

echo $response["Item"]["token"]["S"];

or store in a variable for later use

$res_token = $response["Item"]["token"]["S"];

Upvotes: 3

Related Questions