I have a MySQL database on my website, and I would like to know how I could get a requested XML output via PHP from a table called “customers”.
I have this code and it works but I’d like to name a customer and return me only this one.
<?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();
?>
I would really appreciate it if you could tell me how to do this. Please note; I am a complete newbie at PHP!
Add a where clause to your query. And, like others have said, don’t ever, ever, EVER use the mysql_* functions. Use PDO or mysqli_* instead.