Home  >  Article  >  php教程  >  php代码优化--数据库读取--存成xml格式

php代码优化--数据库读取--存成xml格式

PHP中文网
PHP中文网Original
2016-05-25 17:15:03963browse

php代码优化--数据库读取--存成xml格式

function buildXml($sql, $filepath = '') 
{
    $xml = "";
    $i = 0;
    $rs = mysql_query($sql); //获取数据库资源
    $fp = fopen($filepath, 'w+'); //获取文件资源
    while ($line = mysql_fetch_assoc($rs)) //读取一行
    {
        $xml .= buildOneXmlNode($line);
        $i++;
        if ($i > 0 && $i % 1000 == 0) { //为了避免频繁的io,提高效率,我们这里以1000条记录为限,做了一个缓存。1000条记录大概要迫8M的内存。
            fwrite($fp, $xml);
            $xml = '';
            $i = 0;
        }
    }
    fwrite($fp, $xml . "");//不要忘了这一行,$xml 可能还有没有保存的数据。还有标签要闭合。
    fclose($fp);
}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn