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/>');
?>