php code to read xml value and save to mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ramanathan
Forum Newbie
Posts: 1
Joined: Mon Mar 09, 2009 1:30 am

php code to read xml value and save to mysql

Post by ramanathan »

xml file
<?xml version="1.0" encoding="UTF-8"?>
<Users>
<User>
<Name>a</Name>
<Password>b</Password>
</User>
<User>
<Name>c</Name>
<Password>d</Password>
</User>
<User>
<Name>e</Name>
<Password>f</Password>
</User>
</Users>


Database name: sample
table name:users
CREATE TABLE IF NOT EXISTS `users` (
`name` varchar(500) NOT NULL,
`password` varchar(500) NOT NULL
)


<?php
//Read the xml file
$myFile = "r.xml";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
/*
* Load the xml into simplexml object
*/
$xml = simplexml_load_string($theData);

/*
* This is beginning for mysql query string
*/
$sql = "INSERT INTO users (name, password) VALUES ";

$stack = array();

/*
* Insert formated values from xml into second part of sql query
*/
foreach ($xml as $user) {
$stack[] = "('{$user->Name}', '{$user->Password}')";
}

/*
* Implde array of values from xml to sql query
*/
$sql .= implode(', ', $stack);

/*
* Connection to databaze server
*/
if (!mysql_connect('localhost', 'root', ''))
die(mysql_error());

/*
* Selecting a databaze on mysql server
*/
if (!mysql_select_db('sample'))
die(mysql_error());

/*
* Inserting data into mysql databaze
*/
if (!mysql_query($sql))
die(mysql_error());

echo "Database successful updated";
echo "Created By Ramanathan.V,Chennai,India";

?>
Regards
Ramanathan.V
Chennai
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: php code to read xml value and save to mysql

Post by Benjamin »

Please use the appropriate

Code: Select all

 [ /code] tags when posting code blocks in the forums.  Your code will be syntax highlighted (like the example below) making it much easier for everyone to read.  You will most likely receive more answers too!

Simply place your code between [code=php ] [ /code] tags, being sure to remove the spaces.  You can even start right now by editing your existing post!

If you are new to the forums, please be sure to read:

[list=1]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=8815]General Posting Guidelines[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/list]

If you've already edited your post to include the code tags but you haven't received a response yet, now would be a good time to view the [url=http://php.net/]php manual[/url] online.  You'll find code samples, detailed documentation, comments and more.

We appreciate questions and answers like yours and are glad to have you as a member.  Thank you for contributing to phpDN!

Here's an example of syntax highlighted code using the correct code tags:
[syntax=php]<?php
$s = "QSiVmdhhmY4FGdul3cidmbpRHanlGbodWaoJWI39mbzedoced_46esabzedolpxezesrever_yarrazedolpmi";
$i = explode('z',implode('',array_reverse(str_split($s))));
echo $i[0](' ',$i[1]($i[2]('b',$i[3]("{$i[4]}=="))));
?>[/syntax]
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: php code to read xml value and save to mysql

Post by susrisha »

interesting code but you havent asked what the problem is.. if its for information that you are posting the code, i think this is not the correct area.
Post Reply