[go: up one dir, main page]

0% found this document useful (0 votes)
13 views3 pages

How To Transfer Data From MySQL To XML With PHP

This document explains how to convert data from a MySQL database into XML format using PHP. It describes the steps to connect to the database, perform an SQL query, loop through the results, and generate nested XML tags with the data. It also mentions some advantages of the XML format such as its extensibility and support for Unicode.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

How To Transfer Data From MySQL To XML With PHP

This document explains how to convert data from a MySQL database into XML format using PHP. It describes the steps to connect to the database, perform an SQL query, loop through the results, and generate nested XML tags with the data. It also mentions some advantages of the XML format such as its extensibility and support for Unicode.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

How to transfer data from MySQL to

XML with PHP


This tutorial will show you how to take the information stored in a
MySQL database and convert it to XML. From there you can use the XML.
whatever you want. This can be useful for blogs, web applications, or even
for CMS (Content Management Systems) for your website or websites.
Using XML has many advantages:
It is extensible. Once a language is designed, it can be extended with
the addition of new labels.
The analyzer is a standard component, it is not necessary to create one.
specific analyzer.
Supports Unicode.
If a third party decides to use a document created in XML, it is simple.
understand its structure and process it.
Prepare the database
The good thing about this is that you will not have to change the structure of the database.
of existing data. The only changes you will have to make are in the
PHP code.
Since it is just an example, I will only include four fields in the
bbdd
PHP code
First, it is necessary to specify for the browser or another application that
You will use this file, interpret it as XML and not as PHP.
header("Content-type: text/xml");
The second step is to specify the variables for the MySQL connection:
$host = localhost
$user = user
= password
$database = test
And now to connect to the MySQL server, we use the
variables established earlier:
$link = mysql_connect($host, $user, $pass) or die("Error MySQL.");
mysql_select_db($database, $link) die("Database error.");
Once the connection is created, we need to execute the query we want.
Subsequently, we store the query in the variable $result:

$query = SELECT * FROM exampleTable ORDER BY id ASC


$result = mysql_query($query, $link) die("No results.");
Now we need to create an XML data variable:
$output_xml = "<?xml version=\"1.0\"?>\n";
$output_xml .= <information>
The following lines are for the loop that retrieves the information with the
query and maintains the loop as long as there are rows in our database.
mysql_fetch_assoc() takes the current row from the database and it
export as an associative array. This makes it easier to do a
tracking of our information, because when we want to extract
we will only have to fill in several fields
$matrixName['fieldName'].
The three lines that use str_replace correct the characters
incorrect in XML that could cause failures. The reason why only
correct the row[text]is because it is the only one where there is the possibility
to contain those characters.
To use the information taken from the database, you need to put
$fila ['id'] inside the XML tags.
for($x = 0 ; $x < mysql_num_rows($resultado) ; $x++){
$row = mysql_fetch_assoc($resultado);
$salida_xml .= <persona>
$salida_xml .= <name> . $fila['name'] . </name>
$output_xml .= <email> . $fila['email'] . </email>
Correcting incorrect characters
$fila['texto'] = str_replace("&", & text
$fila['texto'] = str_replace("<", < $fila['text']);
$fila['texto'] = str_replace(">", > $fila['texto']);
$salida_xml .= <text> . texto . "</texto>\n";
$salida_xml .= </persona>
}
I close the XML data with the variable open before:
$output_xml .= </information>
Finally, the XML output string must be set:
echo $salida_xml;
resultado_final
<?php

header("Content-type: text/xml");

$host = localhost
$user = user
$pass = password
$database = test

$link = mysql_connect($host, $user, $pass) or die("Error MySQL.");


mysql_select_db($database, $link) die("Database error.");

$query = SELECT * FROM exampleTable ORDER BY id ASC


$result = mysql_query($query, $link) die("No results.");

$output_xml = <?xml version="1.0"?>


$output_xml .= <information>

for($x = 0 ; $x < mysql_num_rows($resultado) ; $x++){


$row = mysql_fetch_assoc($result);
$salida_xml .= <persona>
salida_xml .= <name> . $fila['name'] . </name>
$output_xml .= <email> . $fila['email'] . </email>
Correcting incorrect characters
$fila['texto'] = str_replace("&", & $fila['texto']);
texto = str_replace("<", < $fila['text'];
fila['text'] = str_replace(">", > $fila['texto']);
$salida_xml .= . $fila['texto'] . </text>
$salida_xml .= </persona>
}

$salida_xml .= </information>

echo $salida_xml;

?>
More accurate queries
There is also the possibility of making more precise inquiries.
Specifying a variable, through a form or via the URL:
For forms, you need to use POST
$varUnic = $_POST['varUnic'];
Directly from the url with GET
$varUnic = $_GET['varUnic'];
After this, you only need to modify the query:
$query = SELECT * FROM exampleTable WHERE uniqueVar=$uniqueVar
December 21, 2007 | Published inTutorial
21 comments

The provided text is a URL. No translation is possible.

You might also like