Creating XML with PHP/MYSQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gavin1211
Forum Newbie
Posts: 6
Joined: Thu Nov 04, 2010 12:47 pm

Creating XML with PHP/MYSQL

Post by gavin1211 »

Would anyone have any idea how I could modify my script so that I can generate an xml file with the nodes displaying title attributes, the title of the node being generated from the mysql data. For example:

<categories>
<category title="Audi">
<courses><courses>
</category>
</categories>

Code: Select all

<?php 

header("Content-type: text/xml"); 

$host = "#"; 
$user = "#"; 
$pass = "#"; 
$database = "#"; 

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

$query = "SELECT * FROM stock ORDER BY Make ASC"; 
$resultID = mysql_query($query, $linkID) or die("Data not found."); 

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

for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){ 
    $row = mysql_fetch_assoc($resultID); 
   $xml_output .= "\t<category>\n"; 
		$xml_output .= "\t\t<title>" . $row['Make'] . "</title>\n"; 
        // Escaping illegal characters 
        $row['Make'] = str_replace("&", "&", $row['Make']); 
        $row['Model'] = str_replace("<", "<", $row['Make']); 
        $row['Price'] = str_replace(">", ">", $row['Make']); 
        $row['Features'] = str_replace("\"", """, $row['Make']); 
    $xml_output .= "\t\t<text>" . $row['Make'] . "</text>\n"; 
    $xml_output .= "\t</category>\n"; 
} 

$xml_output .= "</catergories>"; 
$xml_output .= "</lineup>"; 

echo $xml_output; 

?> 
ssand
Forum Commoner
Posts: 72
Joined: Sat Jun 22, 2002 9:25 pm
Location: Iowa

Re: Creating XML with PHP/MYSQL

Post by ssand »

To get my RSS feed (XML) to work I had to modify the .htaccess file.

Here is what is in my .htaccess file to process the PHP in a .xml file.

Code: Select all

RemoveType .xml
AddType application/x-httpd-php5 .xml
AddHandler application/x-httpd-php5 .xml
-
Post Reply