已经为此密钥指定了一个数据库。多个数据库

时间:2018-08-13 08:39:38

标签: php slim redbean

世界,您好,非常抱歉。但我需要你的帮助

我有这个代码

public function GetSyncTransaksi ($ip_add_Get,$DBIDGet='db_ique',$table_get='transaksi') {
    $temp_get = NULL;
    if ($DBIDGet == 'db_ique') {
        $dbhost_get   = $ip_add_Get;
        $dbuser_get   = USER_CABANG;    //Harus diganti
        $dbpwd_get    = PASS_CABANG;    //Harus diganti
        $dbname_get   = $DBIDGet;
    } else {
        $dbhost_get   = $ip_add_Get;
        $dbuser_get   = USER_PUSAT;     //Harus diganti
        $dbpwd_get    = PASS_PUSAT;     //Harus diganti
        $dbname_get   = "ique_".$DBIDGet;
    }

   R::addDatabase($dbname_get, 'mysql:host='.$dbhost_get.';dbname='.$dbname_get, $dbuser_get, $dbpwd_get, TRUE);
   R::selectDatabase($dbname_get);

    $qget = "SELECT * FROM sync_$table_get";
    $temp_get = R::getAll($qget);

    return $temp_get;
}

public function UpdateSyncTransaksi ($ip_add,$DBID='db_ique',$table='transaksi') {
    $temp = NULL;
    if ($DBID == 'db_ique') {
        $dbhost   = $ip_add;
        $dbuser   = USER_CABANG;
        $dbpwd    = PASS_CABANG;
        $dbname   = $DBID;
    } else {
        $dbhost   = $ip_add;
        $dbuser   = USER_PUSAT;
        $dbpwd    = PASS_PUSAT;
        $dbname   = "ique_".$DBID;
    }

    R::addDatabase($dbname, 'mysql:host='.$dbhost.';dbname='.$dbname, $dbuser, $dbpwd, TRUE);
    R::selectDatabase($dbname);

    $q = "TRUNCATE TABLE sync_$table";
    $temp = R::exec($q);

    $q = "INSERT INTO sync_$table SELECT tanggal,COUNT(*) FROM $table GROUP BY tanggal";
    $temp = R::exec($q);

    return $temp;
}

然后我调用此函数

 public function CompareSyncTransaksi ($server,$id,$table='transaksi') {
    $difference = NULL;

    $updatesynccabang = $this->UpdateSyncTransaksi($server,'db_ique',$table); //Update sync_transaksi cabang
    $updatesyncpusat = $this->UpdateSyncTransaksi("localhost",$id,$table); //Update sync_transaksi pusat

    $synccabang = $this->GetSyncTransaksi($server,'db_ique',$table);
    $syncpusat = $this->GetSyncTransaksi("localhost",$id,$table);

    $tglcabang = NULL;
    if ($synccabang != NULL) {
        foreach ($synccabang as $temp) {
            $tglcabang[] = $temp->tanggal;
        }
    }

    $tglpusat = NULL;
    if ($syncpusat != NULL) {
        foreach ($syncpusat as $temp) {
            $tglpusat[] = $temp->tanggal;
        }
    }

    $intersect = NULL;
    if($tglcabang != NULL && $tglpusat != NULL) {
        $difference = array_diff($tglcabang,$tglpusat);
        $intersect = array_intersect($tglcabang,$tglpusat);
    }

    if($intersect != NULL) {
        foreach ($intersect as $temp) {
            foreach ($synccabang as $temp1) {
                if ($temp1->tanggal == $temp) {
                    $jumlahcabang = $temp1->jumlah;
                }
            }
            foreach ($syncpusat as $temp2) {
                if ($temp2->tanggal == $temp) {
                    $jumlahpusat = $temp2->jumlah;
                }
            }   
            if ($jumlahcabang != $jumlahpusat) {
                $difference[] = $temp;
            }
        }
    }
    if ($difference) {
        asort($difference);
    }

    return $difference;
}

问题是我收到错误消息“已为此密钥指定数据库。”

我认为问题出在函数GetSyncTransaksi和函数UpdateSyncTransaksi。

我该如何解决。请帮忙谢谢你

0 个答案:

没有答案