I'm parsing an CSV to a MySQL table using PHP.
I have timestamp columns in my table "File" called "StartTime" (start of the parsing of the file), "EndTime"(end of parsing of the file) and "CreationDate"(the time the row was created). What ways are there to get these times? Is NOW() a good solution?
I also have a column called "Rows with errors" which is the number of rows that has errors in them and thus won't parse. How can I retrieve the number of rows with errors in the CSV file? (A row with an error would be a row where the types are wrong, for example it should be: [varchar, int, timestamp, timestamp, timestamp]]
Here's my code:
function parse($file) {
$file_handle = fopen($file, "r"); //opens CSV
while (($row = fgetcsv($file_handle, 1000, ",")) !== false){
$file_name = basename($file);
$Rows_with_errors = ?;
$StartDate= date("Y-m-d H:i:s", time() ); //?
$EndDate=?;
$CreationDate=?;
$sql = "INSERT INTO file (Filename, TotalNumberOfRows, RowsWithErrors, StartDate, EndDate, CreationDate)
VALUES('$file_name','$Total_nr_of_Rows', '$Rows_with_errors', '$StartDate', '$EndDate', '$CreationDate')";
global $conn;
$conn->query($sql); //executes the query
}
if ($sql)
{
echo 'Data uploaded to database!';
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}