phpamazon-simpledb

Test for empty result set in Amazon SimpleDB PHP SDK


I've been playing around with integrating a mobile app with SimpleDB and am having an problem testing a Select response for emptiness. I'm not much of an OO PHP programmer and am getting confused looking through the code.

Here's the relevant code to grab all of the items I need:

foreach ($duuids as $duuid) {
  $results = null;
  $select_expression = "SELECT * FROM `thestore` WHERE thing IS NOT NULL AND duuid='" . $duuid . "' ORDER BY thing DESC LIMIT 100";

  $next_token = null;

  do {
    if ($next_token)
    {
        $results = $sdb->select($select_expression, array(
            'NextToken' => $next_token,
        ));
    }
    else
    {
        $results = $sdb->select($select_expression);
    }

      foreach ($results->body->Item() as $item) {
        array_push($items, $item);
      }


      $next_token = isset($results->body->SelectResult->NextToken)
        ? (string) $results->body->SelectResult->NextToken
        : null;
  }
  while ($next_token);
}

My problem is that sometimes there isn't any data uploaded yet for a duuid so the result set is empty which breaks the "foreach ($results->body->Item() as $item)" up there.

According to the SDK Docs the select() returns a CFResponse in which $results->body is a SimpleXML document. I've tried some of the methods to count child objects within a SimpleXML document like "$results->body->SelectResult->count()" but that doesn't seem to be working.

Question:
What's the appropriate way to test for emptiness of this result?


Solution

  • Try this:

    if($results->body->SelectResult->Item()){
      // process the children
    }
    

    You'd think the count() function would work right, but it seems to not do the right thing. Odd that...