doutong7216 2014-09-15 14:38
浏览 82

IE9上的CSV文件生成问题

I'm looking at an odd issue with Internet Explorer 9. The below code successfully generates a populated CSV file when ran in Safari, IE8, IE10, IE11, Chrome and Firefox but generates an empty CSV file (Headers only) in IE9. It does not appear to be a data issue considering the bug only appears to happen in IE9. I've been able to test and confirm on 3 different machines running IE9.

It's a legacy script I inherited with SQLSRV instead of MSSQL PDO, though I took the liberty of scooping out mysql and replacing it with PDO and wrapping it in a function and thoroughly commenting it for my own sanity.

        # Create a CSV sheet with details we need
    # Generate file name based on timestamp
    $file = "$_SESSION[user_name]_" . "tutorincomplete_" . date("Y-m-d_H-i-sa");

    # 1: Open CSV File 
    $myFile = ".\csv\\" . "$file.csv"; 
    $fh = fopen($myFile, 'w') or die("can't open file"); 

    # Get list of active classes from MSSQL DB
    $sql = "SELECT DISTINCT 
    static_title, 
    LEFT(CONVERT(VARCHAR, course_end_date, 120), 150) AS course_end_date, 
    RTRIM(static_code) as static_code, 
    RTRIM(session_code) as session_code,
    staff_name
    FROM 
    snip";

        # Append to query where a school is present
        if (ISSET($_SESSION['user_school']) AND $_SESSION['user_school'] != "None")
        {
        $sql .= "
        WHERE
        school = ?
        ORDER BY staff_name ASC";   
        $params1 = array($_SESSION['user_school']);
        $query = sqlsrv_query($conn, $sql, $params1);
        }
        else
        {
        if (isset($_POST['school']) AND $_POST['school'] != "All") {
        $sql .= "
        WHERE
        school = ?
        ORDER BY staff_name ASC";   
        $params1 = array($_POST['school']);
        $query = sqlsrv_query($conn, $sql, $params1);
        } else {
        $sql .= "
        ORDER BY staff_name ASC";   
        $query = sqlsrv_query($conn, $sql);
        }

        }

        $today = date('U'); 

        # Set headers for CSV file
        $head = array("Static title", "Course end date", "Static code", "Session code", "Staff name");
        fputcsv($fh, $head);
        $count = 0;

            # Loop through MSSQL results for comparison
            while ($obj = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC))
            {

                $finish = $obj['course_end_date'];
                # Strip the time (keep ISO date format intact)
                $finishSub = substr($finish, 0, 10);

                # Create timestamp from date format
                $finishTs = strtotime($finishSub);

                # Compare and display <option> tag if valid
                if ($today > ($finishTs - 604800))
                {

                    # Fetch information on course; Decide of course is complete or incomplete
                    # To do this, first check for course entries in the MySQL database
                    # If they are present, we will use MySQL instead of MSSQL (To reference completed courses)
                    $sql3 = "SELECT * FROM snip WHERE static = :stc AND session = :sec ORDER BY static ASC";
                    $query3 = $pdo->prepare($sql3);
                    $query3->bindParam(':stc', $obj['static_code']);
                    $query3->bindParam(':sec', $obj['session_code']);
                    $query3->execute();
                    $rowCount = $query3->rowCount();


                    # Check for result count in MySQL
                    if ($rowCount > 0)
                    {
                    $result = $query3->fetch(PDO::FETCH_OBJ);

                        if ($result->complete == 0) {
                        $incomplete = 1;
                        } else {
                        $incomplete = 0;
                        $count++;
                        }

                    } else {
                        # No MySQL entry; Mark as incomplete
                        $incomplete = 1;
                    }

                        if ($incomplete > 0)
                        {
                        $arr = array($obj['static_title'], $obj['course_end_date'], $obj['static_code'], $obj['session_code'], $obj['staff_name']);
                        fputcsv($fh, $arr);
                        }

                }

            }

    echo "
    <h1>CSV file generation complete</h1>
    <p>Skipped $count classes</p>
    <p>File generation complete. File name $file.csv <a href=\"./csv/$file.csv\">Click here to open</a></p>";
    }

In anything but IE9, it generates "Skipped X classes", CSV file has x number of entries.

But in IE9, "Skipped 0 classes", CSV file only contains column headers.

EDIT: I found out through some experimentation that SQLSRV is passing an empty array ONLY in IE9. It's not affecting any other browser. As such I was able to impress upon the powers that be that I need to switch the system to PDO for MSSQL. Guess I can call this fixed but unresolved.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题