0

我的网站上有一个 MySQL 数据库,我想知道如何通过 PHP 从名为“customers”的表中获取请求的 XML 输出。

我有这个代码,它可以工作,但我想命名一个客户并只给我这个。

<?php

$con = mysql_connect("localhost", "root", "");
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("eshop");


$query = "select name,address from customer";
$res = mysql_query($query);


$xml = new XMLWriter();

$xml->openURI("php://output");
$xml->startDocument();
$xml->setIndent(true);

$xml->startElement('customers');

while ($row = mysql_fetch_assoc($res)) {
  $xml->startElement("customer");

  $xml->writeAttribute('name', $row['name']);
  $xml->writeAttribute('address', $row['address']);

  $xml->endElement();
}

$xml->endElement();

header('Content-type: text/xml');
$xml->flush();
?>

如果您能告诉我该怎么做,我将不胜感激。请注意; 我是 PHP 的新手!

4

1 回答 1

0

在查询中添加 where 子句。而且,就像其他人所说的那样,永远不要使用 mysql_* 函数。请改用 PDO 或 mysqli_*。

于 2013-01-09T09:43:16.720 回答