0

如何从 SimpleDB 中获取数据?我想做的就是把数据放进去,然后把数据拿出来。看起来数据不容易取出,最终需要计算能力通过循环等方式提取。我说的对吗?

这是我提取数据的代码:

<?php
// Include the SDK
require_once 'sdk.class.php';

// Instantiate
$sdb = new AmazonSDB();

$select_expression = 'SELECT * FROM ' . $domain_name;
$next_token = null;

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

  // Get Data for row 
  $body = $response->body->to_array()->getArrayCopy();

  echo "ID: " . $msgId . "<br>";

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

echo "<br>";
?>

这是我要提取的数据的摘录。

Array
(
  [@attributes] => Array
    (
        [ns] => http://sdb.amazonaws.com/doc/2009-04-15/
    )

[SelectResult] => Array
    (
        [Item] => Array
            (
                [0] => Array
                    (
                        [Name] => 1
                        [Attribute] => Array
                            (
                                [0] => Array
                                    (
                                        [Name] => msgAddress
                                        [Value] => +2782555555
                                    )

                                [1] => Array
                                    (
                                        [Name] => msgType
                                        [Value] => S
                                    )

                                [2] => Array
                                    (
                                        [Name] => msgSubmitDate
                                        [Value] => 2012-09-02 15:48:46
                                    )

                                [3] => Array
                                    (
                                        [Name] => msgText
                                        [Value] => Test SMS message for ID no 1
                                    )

                            )

                    )

                [1] => Array
                    (
                        [Name] => 2
                        [Attribute] => Array
                            (
                                [0] => Array
                                    (
                                        [Name] => msgAddress
                                        [Value] => +27825555555
                                    )

                                [1] => Array
                                    (
                                        [Name] => msgType
                                        [Value] => P
                                    )

                                [2] => Array
                                    (
                                        [Name] => msgSubmitDate
                                        [Value] => 2012-09-02 15:48:46
                                    )

                                [3] => Array
                                    (
                                        [Name] => msgText
                                        [Value] => Test phone message for ID no 2
                                    )

                            )

                    )

                [2] => Array
                    (
                        [Name] => 3
                        [Attribute] => Array
                            (
                                [0] => Array
                                    (
                                        [Name] => msgAddress
                                        [Value] => name@domain.co.za
                                    )

                                [1] => Array
                                    (
                                        [Name] => msgType
                                        [Value] => E
                                    )

                                [2] => Array
                                    (
                                        [Name] => msgSubmitDate
                                        [Value] => 2012-09-02 15:48:46
                                    )

                                [3] => Array
                                    (
                                        [Name] => msgText
                                        [Value] => Test email message for ID no 3 to name@domain.co.za
                                    )

                            )

                    )

                [3] => Array
                    (
                        [Name] => 4
                        [Attribute] => Array
                            (
                                [0] => Array
                                    (
                                        [Name] => msgAddress
                                        [Value] => andrebruton
                                    )

                                [1] => Array
                                    (
                                        [Name] => msgType
                                        [Value] => P
                                    )

                                [2] => Array
                                    (
                                        [Name] => msgSubmitDate
                                        [Value] => 2012-09-02 15:48:46
                                    )

                                [3] => Array
                                    (
                                        [Name] => msgText
                                        [Value] => Test push notification message for ID no 4
                                    )

                            )

                    )

            )

    )
)

我想要得到的是以下变量:

msgId(名称)、msgType、msgAddress、msgText、msgSubmitDate

如果我可以使用 $msgType = Body->SelectResult->Item->Name 或类似的东西来获取它。

4

3 回答 3

3

使用 XPath 的执行速度将提高大约 100 倍。我还没有测试过这段代码,但 XPath 表达式应该非常接近:

$msgAddress = (string) $response->body->query('//Attribute[Name[text()="msgAddress"]]/Value')->first();
于 2012-10-05T07:55:48.203 回答
2

我用一些旧的泰山代码弄清楚了。这适用于二维数组(类似于标准数据库的数据)

这是工作代码:

<?php
// Include the SDK
require_once 'sdk.class.php';

// Instantiate
$sdb = new AmazonSDB();

$select_expression = 'SELECT * FROM ' . $domain_name;
$next_token = null;

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

  // Get Data for row 
  foreach ($response->body->SelectResult->Item as $item) 
    {
        $msgId = $item->Name;
      foreach ($item->Attribute as $attr)
        {
              if ($attr->Name == 'msgAddress') {
                  $msgAddress = $attr->Value;
                }
              if ($attr->Name == 'msgType') {
                  $msgType = $attr->Value;
                }
              if ($attr->Name == 'msgSubmitDate') {
                  $msgSubmitDate = $attr->Value;
                }
              if ($attr->Name == 'msgText') {
                  $msgText = $attr->Value;
                }
        }

      echo "<br>msgId: $msgId<br>";
      echo "msgAddress: $msgAddress<br>";
      echo "msgType: $msgType<br>";
      echo "msgSubmitDate: $msgSubmitDate<br>";
      echo "msgText: $msgText<br><br>";

    }

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

echo "<br>";
?>
于 2012-09-02T16:44:42.707 回答
1
$body = $response->body->to_array()->getArrayCopy();
echo "ID: " . $msgId . "<br>";
//but $msgId is not set
于 2012-09-02T15:32:49.807 回答