0

我正在读取一个 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 productSET 'spr_tech1' = 'S7' WHERE xfield_3IN ERROR: You have an error in your SQL syntax; 检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 ''spr_tech1' = 'S7' WHERE xfield_3IN'附近使用正确的语法

我还尝试使用 json_decode(json_encode($obj), true) 将 XML 转换为数组,但后来我不知道如何访问“条件”节点的 @attributes 字段。

4

1 回答 1

1

您的数据库库期望占位符的字符串数组criteria_list,但您给它一个对象($data->criteria->criteria_list将是 的一个实例SimpleXMLElement)。

您需要做的是使用foreach您为调试编写的循环从 XML 中获取实际字符串:

foreach($data->criteria->criteria_list as $att => $val)

我会重命名这些变量,并写下:

$criteria_strings = [];
foreach($data->criteria->criteria_list as $list_item) {
     $criteria_strings[] = (string)$list_item;
}

然后:

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['field'],
        'criteria_list' => $criteria_strings
    )
);
于 2016-06-03T12:05:24.497 回答