2016-03-23 80 views
0

我正在使用PHP將CSV解析到MySQL表中。獲取sql查詢的開始/結束時間

  1. 我有時間戳列在我的表「文件」名爲「開始時間」(該文件的解析後開始),「結束時間」(文件解析的結束)和「CreationDate」(時間行已創建)。有什麼方法可以獲得這些時間? NOW()是一個很好的解決方案嗎?

  2. 我也有一個名爲「有錯誤的行」的列,這是有錯誤的行數,因此不會被解析。如何檢索CSV文件中有錯誤的行數? (有錯誤的一行將是一個行,所述類型是錯誤的,例如,它應該是:VARCHAR,INT,時間戳,時間戳,時間戳]

這裏是我的代碼:

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; 
} 
} 

回答

0

我從來不運行這個,希望這個作品。解釋是在代碼中。

<?php 

function parse($file) { 

    //Initialize some variables here. Probably in some constants etc. 
    $dateRegex = '|^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$|'; //Just a rough check. I purposely skip the case of like 9999-99-99 99:99:99. Up to you to refine this. 
    $dateFormat = 'Y-m-d H:i:s'; 

    //In fact checking the file exist or not is probably useful too: 
    //if (is_file($file)) ... 

    //Opens CSV in read mode 
    $file_handle = fopen($file, "r"); 

    //We don't need to put this in the loop since it's a one time job 
    $file_name = basename($file); 

    //We also initialize the variable outside the while loop so that we can increment it 
    $Rows_with_errors = 0; 
    $Total_nr_of_Rows = 0; 

    //StartDate of the parsing, shouldn't it be outside of the while loop too? 
    //Also, using "date" will gives you the time in the timezone that you set in your php.ini's date.timezone property. 
    //Using "gmdate" will gives you the time in GMT time. 
    $StartDate = date($dateFormat, time()); 

    while (($row = fgetcsv($file_handle, 1000, ",")) !== false) { 

     ++$Total_nr_of_Rows; 

     //So here, we are looping row by row. Do your checking here: 
     if (!(is_string($row[0]) && 
      is_numeric($row[1]) && 
      preg_match($dateRegex, $row[2]) && 
      preg_match($dateRegex, $row[3]) && 
      preg_match($dateRegex, $row[4]))) { 

       //Increment the error 
       ++$Rows_with_errors; 
     } 
    } 

    //That's it. We insert it now 
    //Creation date & end date, probably the same, no? we initialize here: 
    $CreationDate = $EndDate = date($dateFormat, time()); 

    //This is bad. There is no escaping, and YOU ARE RISK OF QUERY INJECTION FROM THE $file_name VARIABLE. 
    //I won't discuss it here since this is not the scope of the question. 
    $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; 
    } 
} 
+0

它的工作,謝謝! – user212121232323