我正在读取一个 XML 文件,并使用数据通过 MeekroDB 创建查询。通过 XML 读取和解析,一切看起来都很好,并且我确认了变量执行简单的回显。但是,当我构建 DB::query() 时,它似乎没有将“criteria_list”数组添加到查询中。
这是我的 XML 代码:
<?xml version="1.0" encoding="utf-8"?>
<result>
<product>
<data>
<field>spr_tech1</field>
<value>S7</value>
<criteria field="xfield_3">
<criteria_list>Green</criteria_list>
<criteria_list>Beige</criteria_list>
</criteria>
</data>
<data>
<field>spr_tech1</field>
<value>S1</value>
<criteria field="xfield_3">
<criteria_list>Red</criteria_list>
<criteria_list>Blue</criteria_list>
<criteria_list>Yellow</criteria_list>
</criteria>
</data>
<data>
<field>spr_tech1</field>
<value>S7</value>
<criteria field="xfield_3">
<criteria_list>Green</criteria_list>
</criteria>
<criteria field="tech_rt2">
<criteria_list>Transistor</criteria_list>
</criteria>
</data>
</product>
</result>
这是我用来读取和构建查询的代码:
$xml_content = file_get_contents('transformations.xml');
$xform = simplexml_load_string($xml_content);
foreach ($xform->product->data as $data)
{
echo (string)$data->field . '<br>';
echo (string)$data->value . '<br>';
foreach($data->criteria->attributes() as $att => $val)
{ echo $att . ' = ' . $val . '<br>'; }
echo $data->criteria->attributes()->{'field'} . '<br>';
foreach($data->criteria->criteria_list as $att => $val)
{ echo $att . ' = ' . $val . '<br>'; }
echo "-----------------------------------<br>";
}
foreach ($xform->product->data as $data)
{
DB::query("UPDATE %b_table SET %?_new_field = %?_new_value WHERE %b_criteria_field IN %?_criteria_list",
array(
'table' => 'product',
'new_field' => (string)$data->field,
'new_value' => (string)$data->value,
'criteria_field' => (string)$data->criteria->attributes()->{'field'},
'criteria_list' => $data->criteria->criteria_list
)
);
}
print "<pre>";
print_r($xform);
print "</pre>";
当我运行它时,我收到以下错误: QUERY: UPDATE product
SET 'spr_tech1' = 'S7' WHERE xfield_3
IN ERROR: You have an error in your SQL syntax; 检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 ''spr_tech1' = 'S7' WHERE xfield_3
IN'附近使用正确的语法
我还尝试使用 json_decode(json_encode($obj), true) 将 XML 转换为数组,但后来我不知道如何访问“条件”节点的 @attributes 字段。