Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Uploading XML to Mysql using php

Posted on 2011-03-23
4
Medium Priority
?
485 Views
Last Modified: 2012-05-11
Hello Everyone

I am trying to upload a xml file into Mysql using php. The xml file is layed out pretty easy:

<Member>
<Name>Bob</Name>
<other stuff>
</Member>
<Member>
<Name>Jim</Name>
<other stuff>
</Member>

I thought this would be:

$xml = simplexml_load_string($data);
$charname = (string) $xml->Name;
mysql_query("INSERT INTO aTable ( Date,charname, ) VALUES ('$Date','$charname', ') ");
?>

Which inserts one line.I want to be able to insert using the date/time from now and insert Jim and Bob. Could someone give me a example of the Date snippet and how to make it loop from each <member> to the next?





0
Comment
Question by:JCS_Unlimited
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
tsmgeek earned 2000 total points
ID: 35203731
<?php

$data="
<Members>
  <Member>
    <Name>Bob</Name>
  </Member>
  <Member>
    <Name>Jim</Name>
  </Member>
</Members>
";


$xml = simplexml_load_string($data);
foreach($xml->Member as $member){
  $charname = $member->Name;
  $date = date( 'Y-m-d H:i:s' );
  mysql_query("INSERT INTO aTable ( Date,charname ) VALUES ('$Date','$charname');");
}

?>

Open in new window

0
 

Author Comment

by:JCS_Unlimited
ID: 35203801

Thank you tsmgeek,

What if I have a lot more people than just Bob and Jim? and the length of people gets longer or shorter sometimes Will this loop through each of the member sections?
0
 
LVL 5

Expert Comment

by:tsmgeek
ID: 35203858
yes it loops though each member till it gets to the end of the file, all elements under each member are accesable though the variable $member
0
 

Author Comment

by:JCS_Unlimited
ID: 35210303
Worked Great Tsmgeek. Thansk for the help
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question