drdawt9210 2016-03-23 15:37
浏览 115
已采纳

获取sql查询的开始/结束时间

I'm parsing an CSV to a MySQL table using PHP.

  1. 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?

  2. 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;
}
}
  • 写回答

1条回答 默认 最新

  • duandu8892 2016-03-23 15:57
    关注

    I never run this, hope this works. Explanation is within the code.

    <?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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?