|
发表于 2010-10-14 15:32:29
|
显示全部楼层
这样可以吗?
<?php
//set your dabase info here
$conn=mysql_connect("localhost","root","1");
mysql_select_db('a0611200215');
//set your table name here
$tb_name="users";
//get the columns array of the table
$sql="SELECT COLUMN_NAME
FROM information_schema.COLUMNS
WHERE TABLE_NAME='$tb_name'
";
$res=mysql_query($sql);
$col_arr=array();
while($row=mysql_fetch_array($res))
{
$col_arr[]=$row['COLUMN_NAME'];
}
$xml_str='<'.$tb_name.'>';
//you can limit row number or scend
$sql="select * from $tb_name
limit 5";
$res=mysql_query($sql);
while($row=mysql_fetch_assoc($res))
{
foreach($row as $key=>$val)
{
$xml_str.='<'.$key.'>';
$xml_str.=$val;
$xml_str.='</'.$key.'>';
}
}
$xml_str.='</'.$tb_name.'>';
//open a file and write
$filename = 'test.xml';
$somecontent =$xml_str;
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'w')) {
echo "can't open $filename";
exit;
}
if (fwrite($handle, $somecontent) === FALSE) {
echo "can't wite into $filename";
exit;
}
echo "success";
fclose($handle);
}
else
{
echo "can't write";
}
?> |
|