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?
9 4645 @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.
hi devsusen,
thanks for ur reply.
can u give me an example how to do with xml document and php?
there is my example of inserting data from xml to sql databaze - <?php
-
-
/*
-
* Definition of xml
-
*/
-
$example = <<<XML
-
<?xml version="1.0" encoding="UTF-8" ?>
-
<Users>
-
<User>
-
<Name>name1</Name>
-
<Password>password1</Password>
-
</User>
-
<User>
-
<Name>name2</Name>
-
<Password>password2</Password>
-
</User>
-
<User>
-
<Name>name3</Name>
-
<Password>password3</Password>
-
</User>
-
</Users>
-
XML;
-
-
/*
-
* Load the xml into simplexml object
-
*/
-
$xml = simplexml_load_string($example);
-
-
/*
-
* 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('host', 'username', 'password'))
-
die(mysql_error());
-
-
/*
-
* Selecting a databaze on mysql server
-
*/
-
if (!mysql_select_db('database name'))
-
die(mysql_error());
-
-
/*
-
* Inserting data into mysql databaze
-
*/
-
if (!mysql_query($sql))
-
die(mysql_error());
-
-
echo "Databaze successful updated";
-
-
-
?>
REGARDS,
vl4kn0
thanks for ur code.
its really helpful to me.
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.
thank you post code vl4kn0 was reasonable for her to get some change in external XML files independent
file
index.php - <? php
-
$ link = mysql_connect ( "localhost", "root ","");
-
mysql_select_db ( "xmlinsert", $ link);
-
/ *
-
* Connection to server databaze
-
* /
-
if (! mysql_connect ( 'localhost', 'root',''))
-
die (mysql_error ());
-
-
/ *
-
* Selecting a mysql server on databaze
-
* /
-
if (! mysql_select_db ( 'xmlinsert'))
-
die (mysql_error ());
-
-
/ *
-
* Inserting data into mysql databaze
-
* /
-
else
-
(
-
-
$ xml = new SimpleXMLElement ( "phuong_an1.xml", null, true);
-
-
$ sql = "INSERT INTO xmlinsert (name, password) VALUES";
-
-
$ stack = array ();
-
-
foreach ($ xml as $ user) (
-
$ stack [] = "('{$ user-> Name) ',' ($ user-> Password }')";
-
)
-
-
/ *
-
* Implde array of values from xml to sql query
-
* /
-
$ sql .= implode ( ',', $ stack);
-
-
if (! mysql_query ($ sql))
-
die (mysql_error ());
-
-
$ sql = "SELECT *
-
`FROM` xmlinsert
-
LIMIT 0, 30 ";
-
$ returl = mysql_query ($ sql) or die ( "Data not found.");
-
while ($ row = mysql_fetch_array ($ returl)) (
-
-
echo "($ row [name]) <br>";
-
echo "($ row [password]) <br>";
-
)
-
)
-
-
?>
phuong_an1.xml file - <?xml version = "1.0" encoding = "utf-8"?
-
<Users>
-
<user>
-
<name> Nguyen </ Name>
-
<Password> Nguyen passs </ Password>
-
</ User>
-
<user>
-
<name> Nguyen </ Name>
-
<Password> Nguyen passs </ Password>
-
</ User>
-
<user>
-
<name> Nguyen </ Name>
-
<Password> Nguyen passs </ Password>
-
</ User>
-
<user>
-
<name> Nguyen </ Name>
-
<Password> Nguyen passs </ Password>
-
</ User>
-
</ Users>
Markus 6,050
Recognized Expert Expert @bonguyen1
Is that valid XML? As I remember, elements are case-sensitive.
Dormilich 8,658
Recognized Expert Moderator Expert @Markus
they are, stated at the top of the specs
and
is also invalid
@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: -
foreach ($xml as $user) {
-
$stack[] = "('{$user->Name}', '{$user->Password}')";
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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,...
| |
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |