Getting the Value of a count(*) Query in SimpleDB


Summary:

  • AWS SimpleDB queries return a multidimensional object, not a simple value.
  • Look at the returned object to see which property you need and how to reference it.

 
When my app creates users, it firsts checks to see if the username entered by the new user is already in use. I do that with the following query:

$exists = $sdb->select(“select count(*) from `{$user_domain}` where itemName()=’” . $name . “‘ limit 1”);

My assumption was that I could then act based on the value of $exists.

if ($exists == 1) {
// tell the user that the name is taken
} elseif ($exists == 0) {
// great! create the user
}

This was incorrect. What is assigned to $exists is not an integer or a single value. It is an object returned by simpledb, a really long object.

To make this query work like I anticipated, I have to get the correct value from the object. In my case, it was $exists->body->SelectResult->Item->Attribute->Value;

Assigning that to a variable ($exist_int) allowed me to test for the existence of a username based on the results of the query.


Leave a Reply

Your email address will not be published. Required fields are marked *