1 | "REPLACE INTO `tabel` (`een`,`twee`,`drie`) VALUES (:een,:twee,:drie)" |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <?php $db_old = new PDO(...); $db_new = new PDO(...); $res_new = $db_new->query("DESCRIBE `database_nieuw`.`tabel_nieuw`"); $fields = array(); $fields_types = array(); $fields_filling = array(); while ($row_new = $res_new->fetchObject()) { array_push($fields,"`".$row_new->Field."`"); array_push($fields_filling,":".$row_new->Field.""); if (preg_match("/int\(\d+\)/",$row_new->Type)) { $fields_types[$row_new->Field] = PDO::PARAM_INT; } elseif (preg_match("/date|text|varchar/",$row_new->Type)) { $fields_types[$row_new->Field] = PDO::PARAM_STR; } else { $fields_types[$row_new->Field] = PDO::PARAM_STR; } } $to_prepare = "REPLACE INTO `database_nieuw`.`tabel_nieuw` (".implode(",",$fields).") VALUES (".implode(",",$fields_filling).")"; echo "To prepare: " . $to_prepare . "\n"; $res = $db_old->query("SELECT * FROM `database_oud`.`tabel_oud`"); while($row = $res->fetchObject()) { $this_array = array(); $sth = $db_new->prepare($to_prepare); foreach ($fields as $f) { $f2 = preg_replace("/`/","",$f); $sth->bindValue(":".$f2,$row->$f2,$fields_types[$f2]); echo "Binding " . $f2 ." : " .$row->$f2. "\n"; } $sth->execute(); } ?> |
Forum Opties | |
---|---|
Forumhop: | |
Hop naar: |