Tutorial Details
- Judul: Database parse to xml with php
- Difficulty: Beginner
- Platform: -
- Bahasa Scripth: PHP
- Software : Notepad++
- Estimated Completion Time: 15 minutes
- Created:Ricko Nada (Pustaka Flash)
Scripth yang digunakan :
12345678910111213141516171819202122232425262728293031323334<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "nama database kamu";
mysql_connect($server, $username, $password);
mysql_select_db($database);
$namaTabel = "daftarbuku";
header('Content-Type: text/xml');
$query = "SELECT * FROM $namaTabel";
$hasil = mysql_query($query);
$jumField = mysql_num_fields($hasil);
echo "<?xml version='1.0'?>";
echo "<data>";
while ($data = mysql_fetch_array($hasil))
{
echo "<".$namaTabel.">";
for ($i=0; $i<=$jumField-1; $i++)
{
$namaField = mysql_field_name($hasil, $i);
echo "<".$namaField.">".$data[$namaField]."</".$namaField.">";
}
echo "</".$namaTabel.">";
}
echo "</data>";
?>
hasil :
live demo :
1234567891011121314<data>
<buku>
<id>1</id>
<judul>Action scripth 3 dan 2</judul>
<desc>Inilah deskripsi Action scripth 3 dan 2</desc>
</buku>
<buku>
<id>2</id>
<judul>laskar pelangi</judul>
<desc>Inilah deskripsi laskar pelangi</desc>
</buku>
...
</data>
contoh lain
12345678910111213141516171819202122232425262728293031<?php
header("Content-Type: text/xml"); //set the content type to xml
// Initialize the xmlOutput variable
$xmlBody = '<?xml version="1.0" encoding="ISO-8859-1"?>';
$xmlBody .= "<XML>";
// Connect to your MySQL database whatever way you like to here
mysql_connect("DB_HostName","DB_UserName","DB_Password") or die (mysql_error());
mysql_select_db("DB_Name") or die ("no database");
// Execute the Query on the database to select items(20 in this example)
$sql = mysql_query("SELECT * FROM my_table ORDER BY date_time DESC LIMIT 0, 20");
while($row = mysql_fetch_array($sql)){
// Set DB variables into local variables for easier use
$id = $row["id"];
$title = $row["title"];
$date_time = strftime("%b %d, %Y", strtotime($row["date_time"]));
$description = $row["description"];
// Start filling the $xmlBody variable with looping content here inside the while loop
// It will loop through 20 items from the database and render into XML format
$xmlBody .= '
<Data>
<DataID>' . $id . '</DataID>
<DataTitle>' . $title . '</DataTitle>
<DataDate>' . $date_time . '</DataDate>
<DataDescr>' . $description . '</DataDescr>
</Data>';
} // End while loop
mysql_close(); // close the mysql database connection
$xmlBody .= "</XML>";
echo $xmlBody; // output the gallery data as XML file for flash
?>
5 Halaman yang sering dikunjungi:
:Ozz
ReplyDelete