Jump to content

Using Php To Output Mysql As An Xml File?


tomahawk
 Share

Recommended Posts

Hey guys, I have a database that I created using mysql command line. I need to output this data as an XML file, using PHP so that I can take the XML information and use it in an api map. Does anyone know how to do this? Or have a link to a good article on how to do this. Thanks in advance for the help. :)

Link to comment
Share on other sites

You would do the same as if you were outputting information in an HTML file:A quick example is something like this:

<?php// Connect to database// ... header("Content-type: text/xml");echo '<?xml version="1.0"?>';?><root><?php $query = mysql_query("SELECT field1,field2,field3 FROM table");while($row = mysql_fetch_assoc($query)) {  echo '<field1>' . $row['field1'] . '</field1>';  echo '<field2>' . $row['field2'] . '</field2>';  echo '<field3>' . $row['field3'] . '</field3>';}?></root>

Link to comment
Share on other sites

I got it to work. Code is below for those who are curious.

<?php$link = mysqli_connect("localhost", "root", "cdia", "ryan");if (mysqli_connect_errno()) {	printf("Connect failed: %s\n", mysqli_connect_error());	exit();}header('Content-type: text/xml');echo '<?xml version="1.0"?>';?><root><?php $query = "SELECT 			firstName, lastName, city, states, address1, address2, latitude,longitude, country, program, status, photo, portfolio, privacy 		FROM			student";if ($result = mysqli_query($link, $query)) {	while ($row = mysqli_fetch_assoc($result)) {	echo '<firstName>' . $row['firstName'] . '</firstName>'; 	echo '<lastName>' . $row['lastName'] . '</lastName>';	echo '<city>' . $row['city'] . '</city>';	echo '<states>' . $row['states'] . '</states>';	echo '<address1>' . $row['address1'] . '</address1>';	echo '<address2>' . $row['address2'] . '</address2>';	echo '<latitude>' . $row['latitude'] . '</latitude>';	echo '<longitude>' . $row['longitude'] . '</longitude>';	echo '<country>' . $row['country'] . '</country>';	echo '<program>' . $row['program'] . '</program>';	echo '<status>' . $row['status'] . '</status>';	echo '<photo>' . $row['photo'] . '</photo>';	echo '<portfolio>' . $row['portfolio'] . '</portfolio>';	echo '<privacy>' . $row['privacy'] . '</privacy>';	}}				?></root>

Thanks for the help. Much love!!

Edited by tomahawk
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...