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.

Link to comment
Share on other sites

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;
?> 

 

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

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>";

 

 

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.