2012-09-02 34 views
0

如何从SimpleDB中获取数据?我想要做的只是将数据放入数据中,然后将数据输出。它看起来像数据不容易出来,最终将需要通过循环等提取计算能力。我是否正确?使用PHP从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] => [email protected] 
            ) 

           [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 [email protected] 
            ) 

          ) 

        ) 

       [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(名称),信息类型,msgAddress,MSGTEXT,msgSubmitDate

如果我可以用得到$ MSGTYPE =正文 - > SelectResult-> Item-> Name或类似的东西。

+0

我想我需要令牌循环内的另一个循环来循环数据。任何帮助? – andrebruton

回答

1
$body = $response->body->to_array()->getArrayCopy(); 
echo "ID: " . $msgId . "<br>"; 
//but $msgId is not set 
+0

请认真了解$ respone-s的内容print_r($ respone) –

+0

我更新了问题。谢谢 – andrebruton

2

我想出了一些老泰山的代码。这适用于一个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>"; 
?> 
+0

工作代码可在我们的Github https://github.com/andrebruton/SimpleDB-PHP-Example – andrebruton

3

使用XPath将要执行大约快100倍。我还没有测试过这个代码,但XPath表达式应该是非常接近的:

$msgAddress = (string) $response->body->query('//Attribute[Name[text()="msgAddress"]]/Value')->first();