Get MySQL database output via PHP to XML

前端 未结 5 953
失恋的感觉
失恋的感觉 2020-11-27 17:37

I have a MySQL database on my website, and I would like to know how I could get an XML output via PHP of the following columns in the table:

  1. udid
  2. coun
5条回答
  •  悲&欢浪女
    2020-11-27 18:22

    An example with XMLWriter.

    mysql_connect('server', 'user', 'pass');
    mysql_select_db('database');
    
    $sql = "SELECT udid, country FROM table ORDER BY udid";
    $res = mysql_query($sql);
    
    $xml = new XMLWriter();
    
    $xml->openURI("php://output");
    $xml->startDocument();
    $xml->setIndent(true);
    
    $xml->startElement('countries');
    
    while ($row = mysql_fetch_assoc($res)) {
      $xml->startElement("country");
    
      $xml->writeAttribute('udid', $row['udid']);
      $xml->writeRaw($row['country']);
    
      $xml->endElement();
    }
    
    $xml->endElement();
    
    header('Content-type: text/xml');
    $xml->flush();
    

    Output:

    
    
     Country 1
     Country 2
     ...
     Country n
    
    

提交回复
热议问题