thecodedeveloper.com
thecodedeveloper.com

Reputation: 3238

Insert array data into mysql php

i have test.xml file

<propertyList date="2012-05-21-17:12:37" username="" password="">
<business modTime="2011-10-31-12:33:07" status="sold">
  <agentID>TEST</agentID>
  <uniqueID>92134</uniqueID>
  <listingAgent id="1">
    <name>Spiro Abelas</name>
    <telephone type="BH"></telephone>
    <telephone type="mobile">0414298899</telephone>
    <email>[email protected]</email>
  </listingAgent>
  <listingAgent id="2"></listingAgent>
  <address display="no">
    <state>NSW</state>
    <postcode>2203</postcode>
    <country>Australia</country>
  </address>
</business>
</propertyList>

And i did Analyze fully XML like that

<?php
$xml = simplexml_load_file('test.xml');
foreach($xml as $key0 => $value){
echo "..1..[$key0] => $value";
foreach($value->attributes() as $attributeskey0 => $attributesvalue1){
echo "________[$attributeskey0] = $attributesvalue1";
}
echo '<br />';
////////////////////////////////////////////////
foreach($value as $key => $value2){
echo "....2.....[$key] => $value2";
foreach($value2->attributes() as $attributeskey => $attributesvalue2){
echo "________[$attributeskey] = $attributesvalue2";
}
echo '<br />';
////////////////////////////////////////////////
foreach($value2 as $key2 => $value3){
echo ".........3..........[$key2] => $value3";
foreach($value3->attributes() as $attributeskey2 => $attributesvalue3){
echo "________[$attributeskey2] = $attributesvalue3";
}
echo '<br />';
////////////////////////////////////////////////
}}
echo '<br />';
}
?>

Getting those output

  ..1..[business] => ________[modTime] = 2011-10-31-12:33:07________[status] = sold
  ....2.....[agentID] => TEST
  ....2.....[uniqueID] => 92134
  ....2.....[listingAgent] => ________[id] = 1
  .........3..........[name] => Spiro Abelas
  .........3..........[telephone] => ________[type] = BH
  .........3..........[telephone] => 0414298899________[type] = mobile
  .........3..........[email] => [email protected]
  ....2.....[listingAgent] => ________[id] = 2
  ....2.....[address] => ________[display] = no
  .........3..........[state] => NSW
  .........3..........[postcode] => 2203
  .........3..........[country] => Australia

i want to need to fire query like(now it's static but i want make it dyanmic)

insert into xml(business,agentID,uniqueID,listingAgent,name,telephone,email,state,postcode,country)values('sold', 'TEST', 92134, 1, 'Spiro Abelas', 0414298899, '[email protected]', 'NSW', '2203', 'Australia')

so plz guide to how to retrieve data from array and insert into table

Upvotes: 0

Views: 967

Answers (2)

pinaldesai
pinaldesai

Reputation: 1815

Following code will convert your XML to Array.

    $xml    = '<?xml version="1.0" encoding="utf-8"?>
        <propertyList date="2012-05-21-17:12:37" username="" password="">
        <business modTime="2011-10-31-12:33:07" status="sold">
          <agentID>TEST</agentID>
          <uniqueID>92134</uniqueID>
          <listingAgent id="1">
            <name>Spiro Abelas</name>
            <telephone type="BH"></telephone>
            <telephone type="mobile">0414298899</telephone>
            <email>[email protected]</email>
          </listingAgent>
          <listingAgent id="2"></listingAgent>
          <address display="no">
            <state>NSW</state>
            <postcode>2203</postcode>
            <country>Australia</country>
          </address>
        </business>
        </propertyList>
    ';
$xml   = simplexml_load_string($xml);
$json  = json_encode($xml);
$array = json_decode($json,TRUE);

you can execute above code and prinf data like below

print"<pre>"; 
print_r($array );
print_r($array['business']['listingAgent'] );
print"</pre>";

You can Process $array and save in table.

Upvotes: 1

Venu
Venu

Reputation: 7289

Store the data in one array

$data = asort($data);
$keys = array_keys($data);
$values = array_values($data);
$sql = "INSERT INTO tablename (".implode(',',$keys).") VALUES (".implode(',',$values).")";

handing sql injections is left to you.

If you use pdo,

foreach($keys as $key) {
  $str1 .= '?,';
}
$sql = "INSERT INTO tablename (".implode(',',$keys).") VALUES (".substr($str1,0,-1).")";

Upvotes: 0

Related Questions