php - Unable to get DB::query() to work with variables from simpleXML -
i reading xml file, , using data create queries meekrodb. looks xml read , parsing, , confirmed variables doing simple echos. however, when build db::query() doesn't seem adding 'criteria_list' array query.
here 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 code i'm using read , build query:
$xml_content = file_get_contents('transformations.xml'); $xform = simplexml_load_string($xml_content); foreach ($xform->product->data $data) { echo (string)$data->field . '<br>'; echo (string)$data->value . '<br>'; foreach($data->criteria->attributes() $att => $val) { echo $att . ' = ' . $val . '<br>'; } echo $data->criteria->attributes()->{'field'} . '<br>'; foreach($data->criteria->criteria_list $att => $val) { echo $att . ' = ' . $val . '<br>'; } echo "-----------------------------------<br>"; } foreach ($xform->product->data $data) { db::query("update %b_table set %?_new_field = %?_new_value %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 run it, following error: query: update product
set 'spr_tech1' = 's7' xfield_3
in error: have error in sql syntax; check manual corresponds mysql server version right syntax use near ''spr_tech1' = 's7' xfield_3
in' @ line 1
i tried using json_decode(json_encode($obj), true) convery xml array, couldn't figure out how access @attributes field of 'criteria' node.
your db library expecting array of strings criteria_list
placeholder, giving object ($data->criteria->criteria_list
instance of simplexmlelement
).
what need use foreach
loop wrote debugging actual strings out of xml:
foreach($data->criteria->criteria_list $att => $val)
i'd rename variables, , write this:
$criteria_strings = []; foreach($data->criteria->criteria_list $list_item) { $criteria_strings[] = (string)$list_item; }
then:
db::query("update %b_table set %?_new_field = %?_new_value %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 ) );