• 03-11-2022, 21:41:06
    #1
    Merhaba aşağıdaki kod bloğunu kullanıyorum, ama gelen verileri foreach ile dönemedim bir türlü. Yardımcı olabilecek varmı acaba.

    <?php
        try {
    $baglanti = new PDO('mysql:host=localhost;dbname=dbadi', "dbkullanici", "dbsifre");
    $baglanti->exec("SET NAMES 'utf8'; SET CHARSET 'utf8'");
    //read the json file contents
        $jsondata = file_get_contents('https://google.com');
        $data = json_decode($jsondata, TRUE);    
        
        $cityid = $data['data']['0']['CityID'];
        $cityname = $data['data']['0']['CityName'];
        $countpharmacy1 = $data['data']['0']['countPharmacy'];
        $areaname = $data['data']['0']['area'][0]['areaName'];
        $countpharmacy2 = $data['data']['0']['area'][0]['countPharmacy'];
        $name = $data['data']['0']['area'][0]['pharmacy'][0]['name'];
        $phone = $data['data']['0']['area'][0]['pharmacy'][0]['phone'];
        $address = $data['data']['0']['area'][0]['pharmacy'][0]['address'];
        $map = $data['data']['0']['area'][0]['pharmacy'][0]['maps'];
        
        $ayar=$baglanti->prepare("INSERT INTO nobetcieczaneler(cityid, cityname, countpharmacy1, areaname, countpharmacy2, name, phone, address, map)
        VALUES('$cityid', '$cityname', '$countpharmacy1', '$areaname', '$countpharmacy2', '$name', '$phone', '$address', '$map')");
        $ayar->execute(array(2));
        $ayarcek=$ayar->fetch(PDO::FETCH_ASSOC);
        echo "Bağlantınız Başarı İle Gerçekleşti!";
        $baglanti = null;
        } catch (PDOException $e) {
        print "Hata!: " . $e->getMessage() . "<br/>";
    die();
    }
    
    ?>
  • 03-11-2022, 23:45:42
    #2
    INSERT sorgusundan nasıl bir veri dönmesinin bekliyorsunuz?
  • 06-11-2022, 17:18:01
    #3
    örnek json veriyi json haliyle paylaşırsanız daha kolay yardımcı olunabilir
    burdan anlaşılan $json['data'] bir array foreach($json['data'] as $data) olarak dönüp $data['CityId'] ile ulaşabilirsiniz
    ayrıca area ve pharmacy de subarray ler var illa her zaman bu subarray lerin 0 cı elemanını kullanacaksanız $json['data'] yı foreach e sokup $json['data'][0] olan yerleri $data olarak değiştirirseniz kodunuz çalışacaktır.