0

I have a code below

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

The target output of that code is to create 2 csv which is it does but the problem is the 2nd csv has no data although the query shows some it does not write. I tried to copy the 1st line of codes. it does create the file but it didnt write

Whats the problem?

Updated with help of Mr. Barmar

i got this error Commands out of sync; you can't run this command now

22
  • The two queries are the same. Is that really what you wanted? Why not write both files at the same time? Commented Nov 6, 2018 at 3:10
  • yes i did that for testing purposes to check if it does not write on the second one you will see the files where the data will write is different only the query. Actually I will make different CSV from different queries. my problem is when i run the second 1 there is no data created. Commented Nov 6, 2018 at 3:12
  • I can't see any reason why the second file is empty if the query is the same. Commented Nov 6, 2018 at 3:15
  • yeah me too. i dont know why. :( Commented Nov 6, 2018 at 3:16
  • 1
    See this answer regarding this error when calling a stored procedure. Does the procedure perform multiple SELECT queries? Commented Nov 6, 2018 at 3:45

1 Answer 1

1

The stored procedure is apparently returning two result sets. You need to fetch the next result set before you can start another query. Add:

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

After each loop that fetches the results. See https://stackoverflow.com/a/14561639/1491895 for more details.

Sign up to request clarification or add additional context in comments.

1 Comment

tysm :) you made my day complete

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.