Over a million developers have joined DZone.

PHP Script That Returns Records From A Mysql Database In XML Format

·
// description of your code here





';

// Include the database information script:
require_once('mysql.inc.php');

// Connect to the database:
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

// If a connection was established, run the query:
if ($dbc) { 

	// Define the query:
	$q = 'SELECT employees.*, departments.name AS departmentName FROM employees, departments WHERE employees.departmentId = departments.id';

	// Add another conditional if the departmentId was passed to this script:
	if (isset($_GET['departmentId']) && ( ((int)$_GET['departmentId']) > 0 ) ) {
		$q .= ' AND employees.departmentId = ' . (int) $_GET['departmentId'];
	}

	// Run the query:
	$r = mysqli_query($dbc, $q);
	
	// Confirm that some rows were returned:
	if (mysqli_num_rows($r) > 0) {
	
		// Fetch every row and print it as XML:
		while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
			      echo "
			{$row['id']}
			{$row['firstName']}
			{$row['lastName']}
			{$row['email']}
			{$row['phoneExt']}
			{$row['hireDate']}
			{$row['departmentName']}
	
	";
		} // End of WHILE loop.

	} // End of mysqli_num_rows() IF.

} // End of $dbc IF.

// Complete the XML:
echo '';

?>
Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}