PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > how to use php from mysql to xml
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
how to use php from mysql to xml

Réponse
 
LinkBack Outils de la discussion
Vieux 05/01/2008, 10h14   #1
Yang Yang
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut how to use php from mysql to xml

hi,everyone,i am a newbuy for php world

and i have a problem when i study php


i want to make a script,it works for:
a mysql table,like

title author content date
a1 a2 a3 a4
b1 b2 b3 b4
...........................


and i want to use php ,select it and make a xml to save it ,now i use this
script

<?php

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

$host = "localhost";
$user = "root";
$pass = "";
$database = "test";

$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to
host.");
mysql_select_db($database, $linkID) or die("Could not find database.");

$query = "SELECT * FROM blog ORDER BY date DESC";
$resultID = mysql_query($query, $linkID) or die("Data not found.");

$xml_output = "<?xml version=\"1.0\"?>\n";
$xml_output .= "<entries>\n";

for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
$row = mysql_fetch_assoc($resultID);
$xml_output .= "\t<entry>\n";
$xml_output .= "\t\t<date>" . $row['date'] . "</date>\n";
// Escaping illegal characters
$row['text'] = str_replace("&", "&", $row['text']);
$row['text'] = str_replace("<", "<", $row['text']);
$row['text'] = str_replace(">", "&gt;", $row['text']);
$row['text'] = str_replace("\"", "&quot;", $row['text']);
$xml_output .= "\t\t<text>" . $row['text'] . "</text>\n";
$xml_output .= "\t</entry>\n";
}

$xml_output .= "</entries>";

echo $xml_output;

?>

it has no problem,but i want to save a xml file ,like this format

<?xml version="1.0" encoding="GB2312"?>
<Table>
<Record>
<Title>a1</Title>
<Author>a2</Author>
<Content>a3</Content>
<date>2003-06-29</date>
</Record>
<Record>
<Title>b1</Title>
<Author>b2</Author>
<Content>b3</Content>
<date>2003-06-30</date>
</Record>
........
----many record
</Table>



how can i improve this script?

Thanks all

  Réponse avec citation
Vieux 05/01/2008, 11h17   #2
chris smith
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] how to use php from mysql to xml

On Jan 5, 2008 9:14 PM, Yang Yang <baozichn@gmail.com> wrote:
> hi,everyone,i am a newbuy for php world
>
> and i have a problem when i study php
>
>
> i want to make a script,it works for:
> a mysql table,like
>
> title author content date
> a1 a2 a3 a4
> b1 b2 b3 b4
> ..........................
>
>
> and i want to use php ,select it and make a xml to save it ,now i use this
> script



> $resultID = mysql_query($query, $linkID) or die("Data not found.");
> for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
> $row = mysql_fetch_assoc($resultID);


Change that to

while ($row = mysql_fetch_assoc($resultID)) {
...
}

You don't need to know the number of rows the query returns (unless
you actually want to use that number, but you don't need it for this
loop).

> it has no problem,but i want to save a xml file ,like this format


See http://www.php.net/fopen & http://www.php.net/fwrite for details
on how to write to a file.

> <?xml version="1.0" encoding="GB2312"?>
> <Table>
> <Record>
> <Title>a1</Title>
> <Author>a2</Author>
> <Content>a3</Content>
> <date>2003-06-29</date>
> </Record>
> <Record>
> <Title>b1</Title>
> <Author>b2</Author>
> <Content>b3</Content>
> <date>2003-06-30</date>
> </Record>
> ........
> ----many record
> </Table>


Something like this should work:

while ($row = mysql_fetch_assoc($resultID)) {
$xml_entry .= "<record>";
foreach ($row as $fieldname => $data) {
$xml_entry .= "<" . $fieldname . ">";
$xml_entry .= htmlentities($data);
$xml_entry .= "</" . $fieldname . ">";
}
$xml_entry .= "</record>";
}

--
Postgresql & php tutorials
http://www.designmagick.com/
  Réponse avec citation
Vieux 06/01/2008, 04h05   #3
Nathan Nobbe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] how to use php from mysql to xml

On Jan 5, 2008 5:14 AM, Yang Yang <baozichn@gmail.com> wrote:

> hi,everyone,i am a newbuy for php world
>
> and i have a problem when i study php
>
>
> i want to make a script,it works for:
> a mysql table,like
>
> title author content date
> a1 a2 a3 a4
> b1 b2 b3 b4
> ..........................
>
>
> and i want to use php ,select it and make a xml to save it ,now i use this
> script
>
> <?php
>
> header("Content-type: text/xml");
>
> $host = "localhost";
> $user = "root";
> $pass = "";
> $database = "test";
>
> $linkID = mysql_connect($host, $user, $pass) or die("Could not connect to
> host.");
> mysql_select_db($database, $linkID) or die("Could not find database.");
>
> $query = "SELECT * FROM blog ORDER BY date DESC";
> $resultID = mysql_query($query, $linkID) or die("Data not found.");
>
> $xml_output = "<?xml version=\"1.0\"?>\n";
> $xml_output .= "<entries>\n";
>
> for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
> $row = mysql_fetch_assoc($resultID);
> $xml_output .= "\t<entry>\n";
> $xml_output .= "\t\t<date>" . $row['date'] . "</date>\n";
> // Escaping illegal characters
> $row['text'] = str_replace("&", "&", $row['text']);
> $row['text'] = str_replace("<", "<", $row['text']);
> $row['text'] = str_replace(">", "&gt;", $row['text']);
> $row['text'] = str_replace("\"", "&quot;", $row['text']);
> $xml_output .= "\t\t<text>" . $row['text'] . "</text>\n";
> $xml_output .= "\t</entry>\n";
> }
>
> $xml_output .= "</entries>";
>
> echo $xml_output;
>
> ?>
>
> it has no problem,but i want to save a xml file ,like this format
>
> <?xml version="1.0" encoding="GB2312"?>
> <Table>
> <Record>
> <Title>a1</Title>
> <Author>a2</Author>
> <Content>a3</Content>
> <date>2003-06-29</date>
> </Record>
> <Record>
> <Title>b1</Title>
> <Author>b2</Author>
> <Content>b3</Content>
> <date>2003-06-30</date>
> </Record>
> ........
> ----many record
> </Table>
>
> how can i improve this script?



php has tools to you work with xml, the best one for most purposes
in php5 is SimpleXML.
http://www.php.net/manual/en/ref.simplexml.php
heres the way i would do it (you may have debug a tiny bit, all ive done is
verified the syntax parses without errors).

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

$host = "localhost";
$user = "root";
$pass = "";
$database = "test";

$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to
host.");
mysql_select_db($database, $linkID) or die("Could not find database.");

$query = "SELECT * FROM blog ORDER BY date DESC";
if(!($resultSet = mysql_query($query, $linkID))) {
die('data not found!');
}

/// create the table container
$table = new SimpleXMLElement('<table></table>');

while($resultRow = mysql_fetch_object($resultSet)) {
$record = new SimpleXMLElement('<record></record'); // create a
container for the record
foreach($resultRow as $curColName => $curColValue) { // add the
columns from this record
$record->addChild($curColName, $curColValue);
}
$table->addChild($record);
}
echo $table->asXML();


-nathan

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 00h41.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14569 seconds with 11 queries