【问题标题】:Get start/end time of sql query获取sql查询的开始/结束时间
【发布时间】:2016-07-11 00:00:09
【问题描述】:

我正在使用 PHP 将 CSV 解析为 MySQL 表。

  1. 我的表“文件”中有名为“StartTime”(文件解析开始)、“EndTime”(文件解析结束)和“CreationDate”(行的时间)的时间戳列已创建)。有什么方法可以得到这些时间? NOW() 是一个好的解决方案吗?

  2. 我还有一个名为“有错误的行”的列,它是其中有错误因此不会解析的行数。如何检索 CSV 文件中出现错误的行数? (有错误的行就是类型错误的行,例如应该是:[varchar,int,timestamp,timestamp,timestamp]]

这是我的代码:

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

【问题讨论】:

    标签: php mysql csv


    【解决方案1】:

    我从来没有运行过这个,希望它有效。解释在代码中。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-06
      • 2011-03-18
      • 2020-11-09
      • 1970-01-01
      相关资源
      最近更新 更多