duanji2002 2016-06-02 03:55
浏览 53
已采纳

无法让DB :: query()使用simpleXML中的变量

I am reading an XML file, and using the data to create queries with MeekroDB. Everything looks good with the XML read and parsing, and I confirmed the variables doing simple echos. However, when I build the DB::query() it doesn't seem to be adding the 'criteria_list' array to the query.

Here is my XML code:

<?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>

Here is the code I'm using to read and build the query:

$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>";

When I run it, I get the following error: QUERY: UPDATE product SET 'spr_tech1' = 'S7' WHERE xfield_3 IN ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''spr_tech1' = 'S7' WHERE xfield_3 IN' at line 1

I also tried using json_decode(json_encode($obj), true) to convery the XML into an array, but then I couldn't figure out how to access the @attributes field of the 'criteria' node.

  • 写回答

1条回答 默认 最新

  • dongtun2459 2016-06-03 12:05
    关注

    Your DB library is expecting an array of strings for the criteria_list placeholder, but you are giving it an object ($data->criteria->criteria_list will be an instance of SimpleXMLElement).

    What you need to do is use the foreach loop you wrote for debugging to get the actual strings out of the XML:

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

    I'd rename those variables, and write this:

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

    Then:

    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
        )
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值