0

I am selecting all data with the following statement and then store it in a array.

 $SelectCurrentAccInfo = $ConnectToScriptDatabase->prepare("SELECT 
    *
    FROM Accounts WHERE AccountNotes = ?");
    $SelectCurrentAccInfo->bind_param('s',$Notes);
    $SelectCurrentAccInfo->execute();
    $SelectCurrentAccInfo->store_result();
    $accounts = array();
        $SelectCurrentAccInfo->bind_result(
        $accounts["AccountID"],
        $accounts["AccountName"]
       );
    while ($SelectCurrentAccInfo->fetch())
    {
        $CurrentAccInfo = array();
        foreach ($accounts as $key => $val) {
            $CurrentAccInfo[$key] = $val;
        }
        $PostCurrentAccInfo['AccountData'] = $CurrentAccInfo;

    

        echo json_encode($PostCurrentAccInfo);
    }

When I echo the results it returns a array with all the results as expected. What i now want is to leave the loop and post the complete array trough curl. But outside the loop it only shows one row. How do I store the complete array in one variable to post it trough curl?

I do not want the curl in the loop because that will result in a lot of attempts with the single row.

 $ch = curl_init();
    curl_setopt_array($ch, array(
      CURLOPT_URL => 'https://external.site/UpdateAccounts.php',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 1,
      CURLOPT_TIMEOUT => 10,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_POST => true,
      CURLOPT_HEADER => 0,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'POST',
      CURLOPT_POSTFIELDS => json_encode($PostCurrentAccInfo),
    ));
    $DatabaseResponse = curl_exec($ch);
    curl_close($ch);

    echo json_encode($PostCurrentAccInfo); 

On the receiving page I have a script that needs to get this post and do something with it.

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

 

  if (!isset($_POST['AccountData'])) {
    exit('error');
  }

  if (isset($_POST['AccountData'])) {

    echo $_POST['AccountData'];
    print_r($_POST['AccountData']);
  }


}

All help is welcome as i cant find anything simulair to on the stack forum

Martin4523
  • 107
  • 10

0 Answers0