在PHP中创建多个CSV文件

时间:2018-11-06 03:02:27

标签: php opencsv

我下面有一个代码

<?php  
    /* Error display */
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    ini_set('memory_limit', '512M');

    /* Requires */
    require 'conn.php';

    /* Parameters (DIM) */
    $param_customer                = $_POST['param_customer'];
    $param_user                    = $_POST['param_user'];

    /* Others */
    $param_email    = $_POST['email'];
    $file_dump_area = "../general_sync/";

    /* Array */
    $jsonData     = array();
    $arr_result   = array();

    /******************************** Download customer      *********************************/
    $cur_filename = $file_dump_area . removeCharEmail($param_email)  . "_" . $param_customer . ".csv";
    $cur_file     = fopen($cur_filename, "w");
    $cur_sql      = "CALL android_getCustomer('" .$param_email. "')";
    $cur_result   = mysqli_query($con,$cur_sql);

    if ($cur_file && $cur_result) {
        while ($row = $cur_result->fetch_array(MYSQLI_NUM)) {
            fputcsv($cur_file, array_values($row));
        }
        array_push($arr_result, array('done_process' => "done_cus"));
    }
    fclose($cur_file);

    /******************************** Download user          *********************************/
    $cur_filename = $file_dump_area . removeCharEmail($param_email)  . "_" . $param_customer . "1.csv";
    $cur_file     = fopen($cur_filename, "w");
    $cur_sql      = "CALL android_getCustomer('" .$param_email. "')";
    $cur_result   = mysqli_query($con,$cur_sql);

    if ($cur_file && $cur_result) {
        while ($row = $cur_result->fetch_array(MYSQLI_NUM)) {
            fputcsv($cur_file, array_values($row));
        }
        array_push($arr_result, array('done_process' => "done_user"));
    }
    fclose($cur_file);


    $jsonData = array("received"=>$arr_result);
    echo json_encode($jsonData,JSON_PRETTY_PRINT);

    function removeCharEmail($val) {
        $new_val1 = str_replace(".", "", $val);
        $new_val2 = str_replace("@", "", $new_val1);
        return $new_val2;
    }
?>

该代码的目标输出是创建2个csv,但问题是第二个csv没有数据,尽管查询显示它没有写一些内容。我试图复制第一行代码。它确实创建了文件,但没有写入

出什么问题了?

在巴尔玛先生的帮助下进行了更新

我收到此错误Commands out of sync; you can't run this command now

1 个答案:

答案 0 :(得分:1)

存储过程显然返回两个结果集。您需要先获取下一个结果集,然后才能启动另一个查询。添加:

$cur_result->close();
$con->next_result();

在每次获取结果的循环之后。有关更多详细信息,请参见https://stackoverflow.com/a/14561639/1491895