如何从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-&gt; SelectResult-&gt; Item-&gt; Name或类似的东西来获取它。
答案 0 :(得分:3)
使用XPath的速度将提高约100倍。我没有测试过这段代码,但XPath表达式应该非常接近:
$msgAddress = (string) $response->body->query('//Attribute[Name[text()="msgAddress"]]/Value')->first();
答案 1 :(得分: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>";
?>
答案 2 :(得分:1)
$body = $response->body->to_array()->getArrayCopy();
echo "ID: " . $msgId . "<br>";
//but $msgId is not set