Jump to content

[SOLVED] help with fput to save xml to file??


cmaclennan

Recommended Posts

Hi Guys,

 

I have a file that I use to pull data from a mysql database and im looking for some help in being able to execute it to be able to save an xml file manually without having to do that part manually.

 

Can anyone offer some insight on how i may go about this i've heard mention using the fput function just not sure how this would work.

 

Thanks in advance.

 

<?php
//Developed by -==[Mihir Shah]==- during my Project work
//for the output
header("Content-type: text/xml");

//to create connection to database
$connection = mysql_connect("user","pass", "db")
or die ("could not connect to database");

//to select the database here test is the sample database come with mysql
$db = mysql_select_db("powercart",$connection)
or die ("Couldn't select database.");

$rs = mysql_query("SELECT * FROM customers",$connection)
or die ("invalid query");

//count the no. of  columns in the table
$fcount = mysql_num_fields($rs);

//you can choose any name for the starting tag
echo ("<customers>");
while($row = mysql_fetch_array( $rs ) )
{
echo ("<customer>");
for($i=0; $i< $fcount; $i++)
{
$tag = mysql_field_name( $rs, $i );
echo ("<$tag>". $row[$i]. "</$tag>");
}
echo ("</customer>");
}
echo ("</customers>");
?> 

 

Here's what i have that generates the xml in the browser and it works great just trying to automate it a bit.

Try writing the contents to a file by using the fwrite function:

 

$file = fopen("MyXML.xml", "w"); //File is created if it doesn't exist already, and it is overwritten if it does.

fwrite($file, $myXML); //$ Outputs the contents of $myXML to the MyXML.xml file.

did i miss something or do something wrong i added the code you proovided and got this error:

Only one top level element is allowed in an XML document. Error processing resource 'http://solidweb/powercart/cust_xml.php...

 

 

This generates the xml and echos it the browser

<?php
ob_start();
//Developed by -==[Mihir Shah]==- during my Project work
//for the output
header("Content-type: text/xml");

//to create connection to database
$connection = mysql_connect("user","pass", "db")
or die ("could not connect to database");

//to select the database here test is the sample database come with mysql
$db = mysql_select_db("powercart",$connection)
or die ("Couldn't select database.");

$rs = mysql_query("SELECT * FROM customers",$connection)
or die ("invalid query");

//count the no. of  columns in the table
$fcount = mysql_num_fields($rs);

$h = ob_get_clean();
//you can choose any name for the starting tag
echo ("<customers>");
while($row = mysql_fetch_array( $rs ) )
{
echo ("<customer>");
for($i=0; $i< $fcount; $i++)
{
$tag = mysql_field_name( $rs, $i );
echo ("<$tag>". $row[$i]. "</$tag>");
}
echo ("</customer>");
}
echo ("</customers>");

$xml = ob_get_clean();
$fp = fopen("document.xml", "w");
if($fp)
{
fwrite($fp, $xml);
}

echo $h.$xml
?> 

 

or this

 

<?php
//Developed by -==[Mihir Shah]==- during my Project work
//for the output
header("Content-type: text/xml");

//to create connection to database
$connection = mysql_connect("user","pass", "db")
or die ("could not connect to database");

//to select the database here test is the sample database come with mysql
$db = mysql_select_db("powercart",$connection)
or die ("Couldn't select database.");

$rs = mysql_query("SELECT * FROM customers",$connection)
or die ("invalid query");

//count the no. of  columns in the table
$fcount = mysql_num_fields($rs);

//you can choose any name for the starting tag
$xml = "<customers>";
while($row = mysql_fetch_array( $rs ) )
{
$xml .= "<customer>";
for($i=0; $i< $fcount; $i++)
{
$tag = mysql_field_name( $rs, $i );
$xml .= "<$tag>". $row[$i]. "</$tag>";
}
$xml .= "</customer>";
}
$xml .= "</customers>";


$fp = fopen("document.xml", "w");
if($fp)
{
fwrite($fp, $xml);
}

//remove this if you dont need to output to the browser
echo $xml;
?> 

 

I'm also back to getting the "Only one top level element" error which shows up near the bottom of the page for some reason.

 

Since the page is short here it is.

 

<?php
//for the output
header("Content-type: text/xml");

//to create connection to database
$connection = mysql_connect("localhost","powercart", "powercart")
or die ("could not connect to database");

//to select the database here test is the sample database come with mysql
$db = mysql_select_db("powercart",$connection)
or die ("Couldn't select database.");

$rs = mysql_query("SELECT * FROM customers",$connection)
or die ("invalid query");

//count the no. of  columns in the table
$fcount = mysql_num_fields($rs);

//you can choose any name for the starting tag
echo ("<customers>");
while($row = mysql_fetch_array( $rs ) )
{
echo ("<customer>");
for($i=0; $i< $fcount; $i++)
{
$tag = mysql_field_name( $rs, $i );
echo ("<$tag>". $row[$i]. "</$tag>");
}
echo ("</customer>");
}
echo ("</customers>");

$fp = fopen("document.xml", "w");
if($fp)
{
fwrite($fp, $xml);
}

//remove this if you dont need to output to the browser
echo $xml;
?>

change

 

echo ("<customers>");
while($row = mysql_fetch_array( $rs ) )
{
echo ("<customer>");
for($i=0; $i< $fcount; $i++)
{
$tag = mysql_field_name( $rs, $i );
echo ("<$tag>". $row[$i]. "</$tag>");
}
echo ("</customer>");
}
echo ("</customers>");

 

to

 

$xml = "<customers>";
while($row = mysql_fetch_array( $rs ) )
{
$xml .= "<customer>";
for($i=0; $i< $fcount; $i++)
{
$tag = mysql_field_name( $rs, $i );
$xml .= "<$tag>". $row[$i]. "</$tag>";
}
$xml .= "</customer>";
}
$xml .= "</customers>";

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.