Json array to php mysql

I want to save these data coming from API, coin names, which are “assets”, and balances that are “free” to MYSQL. However, it adds an empty row to the database. code as below. How should I do?

API view;

Array ( [makerCommission] => 10 [takerCommission] => 10 [buyerCommission] => 0 [sellerCommission] => 0 [canTrade] => 1 [canWithdraw] => 1 [canDeposit] => 1 [updateTime] => 1615737088938 [accountType] => SPOT [balances] => Array ( [0] => Array ( [asset] => BTC [free] => 0.00000001 [locked] => 0.00000000 ) [1] => Array ( [asset] => LTC [free] => 0.00000000 [locked] => 0.00000000 ) [2] => Array ( [asset] => ETH [free] => 0.00000000 [locked] => 0.00000000 ) [3] => Array ( [asset] => NEO [free] => 0.00000000 [locked] => 0.00000000 ) [4] => Array ( [asset] => BNB [free] => 0.00066371 [locked] => 0.00000000 ) 

MY CODE;

    $execResult = curl_exec($ch);
    $Balances = json_decode($execResult, true); 
    foreach($Balances as $row);
      
    {
    $query .= "INSERT INTO table (asset, free, locked) VALUES('" . $row["asset"] . "', '" . $row["free"] . "', '" . $row["locked"] . "')";
    if(mysqli_multi_query($coonect, $query)) 

Answer

The balances array you are accessing is another level down in $Balances so you should be using

foreach($Balances['balances'] as $row)

and you should be using prepared statements to protect yourself from SQL injection attacks.

Leave a Reply

Your email address will not be published. Required fields are marked *