Re: how to use php from mysql to xml

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Jan 5, 2008 9:14 PM, Yang Yang <baozichn@xxxxxxxxx> 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/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux