473,473 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to insert and select the records from xml document to mysql using php?

paulrajj
47 New Member
hi everybody,

i am newbie to php and mysql. i have a little bit knowledge about php with xml. how to insert and select the records from xml to mysql using php?
Feb 23 '09 #1
9 4645
devsusen
136 New Member
@paulrajj
Not very clear to me, but looks like inserting data from XML to MySql using php.
For this you need the read the XML data and generate sql Insert qureies and run mysql_query() to insert the data into MySql.
Feb 23 '09 #2
paulrajj
47 New Member
hi devsusen,

thanks for ur reply.
can u give me an example how to do with xml document and php?
Feb 23 '09 #3
vl4kn0
5 New Member
there is my example of inserting data from xml to sql databaze

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. /*
  4.  * Definition of xml
  5.  */
  6. $example = <<<XML
  7. <?xml version="1.0" encoding="UTF-8" ?>
  8. <Users>
  9.     <User>
  10.         <Name>name1</Name>
  11.         <Password>password1</Password>
  12.     </User>
  13.     <User>
  14.         <Name>name2</Name>
  15.         <Password>password2</Password>
  16.     </User>
  17.     <User>
  18.         <Name>name3</Name>
  19.         <Password>password3</Password>
  20.     </User>
  21. </Users>
  22. XML;
  23.  
  24. /*
  25.  * Load the xml into simplexml object
  26.  */
  27. $xml = simplexml_load_string($example);
  28.  
  29. /*
  30.  * This is beginning for mysql query string
  31.  */
  32. $sql = "INSERT INTO users (name, password) VALUES ";
  33.  
  34. $stack = array();
  35.  
  36. /*
  37.  * Insert formated values from xml into second part of sql query
  38.  */
  39. foreach ($xml as $user) {
  40.     $stack[] = "('{$user->Name}', '{$user->Password}')";
  41. }
  42.  
  43. /*
  44.  * Implde array of values from xml to sql query
  45.  */
  46. $sql .= implode(', ', $stack);
  47.  
  48. /*
  49.  * Connection to databaze server
  50.  */
  51. if (!mysql_connect('host', 'username', 'password'))
  52.     die(mysql_error());
  53.  
  54. /*
  55.  * Selecting a databaze on mysql server
  56.  */
  57. if (!mysql_select_db('database name'))
  58.     die(mysql_error());
  59.  
  60. /*
  61.  * Inserting data into mysql databaze
  62.  */
  63. if (!mysql_query($sql))
  64.     die(mysql_error());
  65.  
  66. echo "Databaze successful updated";
  67.  
  68.  
  69. ?>
REGARDS,
vl4kn0
Feb 23 '09 #4
paulrajj
47 New Member
thanks for ur code.
its really helpful to me.
Feb 23 '09 #5
Dormilich
8,658 Recognized Expert Moderator Expert
NOTE:
depending on the complexity of your code you can also use:
- the DOMDocument class for XML (if you have namespaces or want to use XPath)
- a Database Abstraction Layer (PDO, MDB2, MySQLi), where you can use arrays for input (e.g. in prepared statements)

if you're in an experimental mood, you can also try to form the SQL directly from the XML using XSLT.
Feb 23 '09 #6
bonguyen1
1 New Member
thank you post code vl4kn0 was reasonable for her to get some change in external XML files independent
file
index.php
Expand|Select|Wrap|Line Numbers
  1. <? php
  2. $ link = mysql_connect ( "localhost", "root ","");
  3. mysql_select_db ( "xmlinsert", $ link);
  4. / *
  5.    * Connection to server databaze
  6.    * /
  7.   if (! mysql_connect ( 'localhost', 'root',''))
  8.       die (mysql_error ());
  9.  
  10.   / *
  11.    * Selecting a mysql server on databaze
  12.    * /
  13.   if (! mysql_select_db ( 'xmlinsert'))
  14.       die (mysql_error ());
  15.  
  16.   / *
  17.    * Inserting data into mysql databaze
  18.   * /
  19.   else
  20.   (
  21.  
  22. $ xml = new SimpleXMLElement ( "phuong_an1.xml", null, true);
  23.  
  24.   $ sql = "INSERT INTO xmlinsert (name, password) VALUES";
  25.  
  26.   $ stack = array ();
  27.  
  28.   foreach ($ xml as $ user) (
  29.       $ stack [] = "('{$ user-> Name) ',' ($ user-> Password }')";
  30.   )
  31.  
  32.   / *
  33.   * Implde array of values from xml to sql query
  34.    * /
  35.   $ sql .= implode ( ',', $ stack);
  36.  
  37.   if (! mysql_query ($ sql))
  38.       die (mysql_error ());
  39.  
  40.    $ sql = "SELECT *
  41. `FROM` xmlinsert
  42. LIMIT 0, 30 ";
  43. $ returl = mysql_query ($ sql) or die ( "Data not found.");
  44. while ($ row = mysql_fetch_array ($ returl)) (
  45.  
  46.   echo "($ row [name]) <br>";
  47.   echo "($ row [password]) <br>";
  48.   )
  49.    )
  50.  
  51.   ?>
phuong_an1.xml file
Expand|Select|Wrap|Line Numbers
  1. <?xml version = "1.0" encoding = "utf-8"?
  2.      <Users>
  3.         <user>
  4.             <name> Nguyen </ Name>
  5.            <Password> Nguyen passs </ Password>
  6.        </ User>
  7. <user>
  8.             <name> Nguyen </ Name>
  9.            <Password> Nguyen passs </ Password>
  10.        </ User>
  11. <user>
  12.             <name> Nguyen </ Name>
  13.            <Password> Nguyen passs </ Password>
  14.        </ User>
  15. <user>
  16.             <name> Nguyen </ Name>
  17.            <Password> Nguyen passs </ Password>
  18.        </ User>
  19.   </ Users>
Sep 4 '09 #7
Markus
6,050 Recognized Expert Expert
@bonguyen1
Is that valid XML? As I remember, elements are case-sensitive.
Sep 4 '09 #8
Dormilich
8,658 Recognized Expert Moderator Expert
@Markus
they are, stated at the top of the specs

and
Expand|Select|Wrap|Line Numbers
  1. </ name>
is also invalid
Sep 4 '09 #9
vl4kn0
5 New Member
@bonguyen1
where did you get this code? all blocks (while, for, if, else if, else, foreach, function definition, class definition) have to be in curly brackets -> {}
for example:
Expand|Select|Wrap|Line Numbers
  1. foreach ($xml as $user) {
  2.     $stack[] = "('{$user->Name}', '{$user->Password}')";
  3. }
  4.  
Sep 5 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: David Bordas | last post by:
Hi list, I've got a little bug with MySQL. I can insert a row into my table but this row will not appear in the table :( Server is under linux redhat, MySQL is 3.23.56 installed from binary...
0
by: Lars Rasmussen | last post by:
I tried that, but i dont work either. I need to insert a way that mysql doese'nt complain when i copy some records that have the same id (or that it just gives it an id according to the...
11
by: Randell D. | last post by:
Folks, I have a table of addresses and a seperate table with contact names - All addresses tie to one or more names - I would like to keep track of the number of names 'belonging' to an address...
0
chumlyumly
by: chumlyumly | last post by:
Hello scripters - OS: Mac OSX Language: PHP w/ MySQL database I've created an insert page where a user inputs his info, which then goes to four different tables in a MySQL database. The...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.