I am using PHP code to upload the data from multiple CSV files into database.
Whenever I am inserting the records from CSV file, I want to maintain a separate logs for the records that were unable to insert into database. For eg In above CSV file,if records 2,3 and 4 were inserted into database but 5 was not inserted because of some reasons, I want to keep a separate log for that. How can I achieve that?
Here is my sample CSV file data:
Here is my php code:
$csvFile= fopen($dir.'/'.$file,"r");
while (($line = fgetcsv($csvFile)) !== FALSE) {
// Get row data
$account_code = $line[0];
$caller_number = $line[1];
$callee_number = $line[2];
$context = $line[3];
$calerid = $line[4];
$source_channel=$line[5];
$dest_channel=$line[6];
$lastapp=$line[7];
$lastdata=$line[8];
$start_time=$line[9];
$answer_time=$line[10];
$end_time=$line[11];
$query = $db->query
("INSERT INTO `cdrnew`(`account_code`, `caller_number`, `callee_number`, `context`, `calerid`, `source_channel`, `dest_channel`, `lastapp`, `lastdata`, `start_time`, `answer_time`, `end_time`)
VALUES ('".$account_code."', '".$caller_number."', '".$callee_number."', '".$context."', '".$calerid."','".$source_channel."','".$dest_channel."','".$lastapp."','".$lastdata."','".$start_time."','".$answer_time."','".$end_time."')");
}
fclose($csvFile);
If the insert fails, the $query
will return false so,
if(!$query) {
// log
file_put_contents('<path to log directory>'.date("j.n.Y").'.log', '<error to log>', FILE_APPEND);
}
Capturing failed $query = $db -> query
:
if($query) // returns true if successful
{
// log success
} else {
// log failed
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.