Jump to content

XML Parsing in PHP/Query


Barbo

Recommended Posts

Hello I'm having some trouble with a certain PHP file. I have a database created on my local server and need to use PHP to generate XML from the tables in the database.

 

The code works when I try to retrieve data from only one table but I can't seem to figure out how to query multiple tables.

 

This is a snippet of the code:

 

$connection = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
mysql_select_db($database, $connection) or die("Could not find database.");

$query = "SELECT * FROM department";
$resultSet = mysql_query($query, $connection) or die("Data not found.");


$xml_output = "<?xml version=\"1.0\"?>\n";
$xml_output .= "<school>\n";

for($x = 0 ; $x < mysql_num_rows($resultSet) ; $x++){
    $row = mysql_fetch_assoc($resultSet);
    $xml_output .= "\t<department>\n";
    $xml_output .= "\t\t<name>" . $row['name'] . "</name>\n";
    $xml_output .= "\t\t<code>" . $row['code'] . "</code>\n";
    $xml_output .= "\t</department>\n";

    $xml_output .= "\t<program>\n";
    $xml_output .= "\t\t<name>" . $row['name'] . "</name>\n";
    $xml_output .= "\t\t<department>" . $row['department'] . "</department>\n";
    $xml_output .= "\t\t<co-op>" . $row['co-op'] . "</co-op>\n";
    $xml_output .= "\t</program>\n";

 

The department table contains values of name and code.

 

The program table contains values of name, department and co-op.

 

My question is how would I go about printing all of the contents of  the department and program tables?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.