dqlk31541 2014-07-22 06:17
浏览 26

以前的文件值也会附加在php中逐个写入的xml文件中

I have a code with which generates new xml files from records coming from database table in php.

I want to make 10 xml files of 10k records each; so I'm setting limit in sql query incrementing them by 10k. But when I create xml first xml file, it makes file with 10k records.

But after that when I create the next xml file, it appends previous files records as well.

Like the first file will be having 10k records, then next will be having 20k records, and next 30k records and so on till tenth file which will be having 100k records.

But I want that all files should have 10k records starting from 1 till 10th file. First 10k records in first file and next 10k records in second file.next 10k records in third file and so on.

So in the end, all files will be having 10k records each.

The code is below, please guide me on what I need to change. Thank you.

      <?php

        // Create Database connection
        $mysqli = new mysqli('localhost', 'user', 'pass', 'dbname');

        if(mysqli_connect_errno()) {
            echo "Connection Failed: " . mysqli_connect_errno();
        }

        /* if (!$mysqli->set_charset("utf8")) {
            printf("Error loading character set utf8: %s
", $mysqli->error);
        } else {
            printf("Current character set: %s
", $mysqli->character_set_name());
        }
        */

        for ($i = 0; $i < 10; $i++) {
        // used to be: $query='select tablecolname1 from tablename limit 10000';
        $start = $i * 10000;
        $query = "select tablecolname1 from tablename limit $start, 10000";
    $result = mysqli_query($mysqli,$query);  

    //Create SimpleXMLElement object
    $xml = new SimpleXMLElement('<urlset/>');
while($row = mysqli_fetch_array($result)) {
        $mydata = $xml->addChild('url');
$mydata->loc=htmlentities($row['Siteurl']);
//htmlentities(strip_tags($mydata->loc=$row['Siteurl']), ENT_COMPAT,'utf-8');
    }
        // used to be: $fp = fopen("folder/file2.xml","wb");
        $fp = fopen("folder/file_$i.xml","wb");
        fwrite($fp,utf8_encode($xml->asXML()));
        fclose($fp);
    }



        $xml = new SimpleXMLElement('<urlset/>');

        ?>
  • 写回答

1条回答 默认 最新

  • dougai6464 2014-07-22 06:38
    关注

    MySQL limit statement takes offset and row_count, instead of offset_start, offset_end. Also I suggest using a for loop to output the 10 files like this:

    // after establishing MySQL connection
    for ($i = 0; $i < 10; $i++) {
        // used to be: $query='select tablecolname1 from tablename limit 10000';
        $start = $i * 10000;
        $query = "select tablecolname1 from tablename limit $start, 10000";
        ...
        // used to be: $fp = fopen("folder/file2.xml","wb");
        $fp = fopen("folder/file_$i.xml","wb");
        ...
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助