krishnakanth ks krishnakanth ks - 14 days ago 5
PHP Question

dynanmo db filterexpression with multiple condition php

I am unable to retrieve the item using filterExpression using logical

and
or
or
in Dyanamodb php using aws sdk. can any any one provide the code with the correct syntax to retrieve the item satisfying both the condition.

$scan_response = $dynamodb->scan(array(
'TableName' => $tableName,
'ExpressionAttributeValues' => [
':val1' => ['S' => '20'],
':val2' => ['S' => 'ajay'],

'FilterExpression' => 'age = :val1 AND name = :val2'
]));


i am getting error that


filterExpression must be a associative array


Thanks in advance

Answer

try this

$scan_response = $dynamodb->scan(array(
    'TableName' => $tableName,
    'ExpressionAttributeValues' => [
        ':val1' => ['S' => '20'],
        ':val2' => ['S' => 'ajay']],

    'FilterExpression' => 'age = :val1 AND name = :val2'
));

badly placed closing ] creates

array(2) {
  ["TableName"]=>
  string(3) "..."
  ["ExpressionAttributeValues"]=>
  array(3) {
    [":val1"]=>
    array(1) {
      ["S"]=>
      string(2) "20"
    }
    [":val2"]=>
    array(1) {
      ["S"]=>
      string(4) "ajay"
    }
    ["FilterExpression"]=>
    string(28) "age = :val1 AND name = :val2"
  }
}

instead of required

array(3) {
  ["TableName"]=>
  string(3) "..."
  ["ExpressionAttributeValues"]=>
  array(2) {
    [":val1"]=>
    array(1) {
      ["S"]=>
      string(2) "20"
    }
    [":val2"]=>
    array(1) {
      ["S"]=>
      string(4) "ajay"
    }
  }
  ["FilterExpression"]=>
  string(28) "age = :val1 AND name = :val2"
}
Comments