$v) $data[$k] = clean_data_recursive_pre_insert($v); }else{ $data = clean_data_pre_insert($data); } return $data; } function clean_data_pre_insert($data){ $data = trim($data); $data = addslashes($data); return $data; } function retrieve_columns($tbl, $db){ $cols = array(); $rows = $db->query('SELECT * FROM `'. $tbl .'` LIMIT 0,1;'); $row = array_shift($rows); foreach($row as $col=>$val){ $cols[$col] = $col; } return $cols; } function getDateUdp($data){ return $data['date_add']; } function getIdProd($data){ global $olddb; $q = 'SELECT `id_product` FROM `ps_image` WHERE `id_image`="'. $data['id_image'] .'";'; $id_products = $olddb->query($q); return $id_products[0]['id_product']; } require "lib/meekrodb.php"; $errors = array(); $settings =array(); $settings['olddb_host'] = 'HÖTE ANCIEN SITE'; $settings['olddb_name'] = 'BD ANCIEN SITE'; $settings['olddb_user'] = 'UTILISATEUR BD ANCIEN SITE'; $settings['olddb_pass'] = 'MOT DE PASSE BD ANCIEN SITE'; $settings['newdb_host'] = 'HÖTE NOUVEAU SITE'; $settings['newdb_name'] = 'BD NOUVEAU SITE'; $settings['newdb_user'] = 'UTILISATEUR BD NOUVEAU SITE'; $settings['newdb_pass'] = 'MOT DE PASSE BD NOUVEAU SITE'; $olddb = new MeekroDB($settings['olddb_host'], $settings['olddb_user'], $settings['olddb_pass'], $settings['olddb_name']); $newdb = new MeekroDB($settings['newdb_host'], $settings['newdb_user'], $settings['newdb_pass'], $settings['newdb_name']); $tables = array(); $tables[] = array( 'table'=>'ps_customer_message', 'id'=>'id_customer_message', 'fields'=>array('id_customer_thread' => 'id_customer_thread', 'id_employee' => 'id_employee', 'message' => 'message', 'file_name' => 'file_name', 'ip_address' => 'ip_address', 'user_agent' => 'user_agent', 'private' => 'private', 'read' => 'read', 'date_add' => 'date_add', 'user_subject' => 'user_subject', 'ordernumber' => 'ordernumber', 'user_product_name' => 'user_product_name', 'date_add2'=>array('date_upd', 'getDateUdp')), 'truncate'=>0 ); $tables[] = array( 'table'=>'ps_product_tag', 'id'=>'id_tag', 'fields'=>array('id_product'=>'id_product', 'id_tag'=>'id_tag', 'id_lang'=>array('id_lang', 1)), 'truncate'=>1 ); $tables[] = array( 'table'=>'ps_image_shop', 'id'=>'id_image', 'fields'=>array('id_image'=>'id_image', 'id_shop'=>'id_shop', 'cover'=>'cover', 'id_product'=>array('id_product', 'getIdProd')), 'truncate'=>1 ); /** * EX: table all field $tables[] = array( 'table'=>'ps_image_shop', 'id'=>'id_image', 'fields'=>array(), 'truncate'=>1 ); */ foreach($tables as $table){ $errors = array(); $oldrows = $olddb->query('SELECT * FROM `'. $table['table'] .'`;'); $queries = array(); if( (isset($table['truncate'])) && ($table['truncate'] == 1) ){ $newdb->query('TRUNCATE TABLE `'. $table['table'] .'`;'); } if(empty($table['fields'])) $table['fields'] = retrieve_columns($table['table'], $olddb); foreach($oldrows as $oldrow){ $q = 'SELECT `'. $table['id'] .'` FROM `'. $table['table'] .'` WHERE `'. $table['id'] .'` = \''. $oldrow[$table['id']] .'\';'; $newrows = $newdb->query($q); $oldrow = clean_data_recursive_pre_insert($oldrow); $query = ''; foreach($table['fields'] as $of=>$nf){ if(isset($oldrow[$of]) || is_array($nf)){ if(is_array($nf)){ $f = $nf[0]; if(function_exists( $nf[1])){ if(isset($oldrow[$of])) $val = call_user_func($nf[1], $oldrow[$of]); else $val = call_user_func($nf[1], $oldrow); }else{ $val = $nf[1]; } }else{ $f = $nf; $val = $oldrow[$of]; } if($f != $table['id']) $query .= ' `'. $f.'`=\''. $val .'\','; } } $query = rtrim($query, ','); if($query != ''){ if(!empty($newrows)){ //UPDATE $query = 'UPDATE `'. $table['table'] .'` SET'. $query .' WHERE `'. $table['id'] .'` = \''. $oldrow[$table['id']] .'\';'; }else{ //INSERT $query = 'INSERT INTO `'. $table['table'] .'` SET'. $query .', `'. $table['id'] .'` = \''. $oldrow[$table['id']] .'\';'; } $queries[] = $query; } } foreach($queries as $q){ if(!$newdb->query($q)) $errors[] = $q; } if(count($errors) > 0) echo "

Errors:[". $table['table'] ."]

". print_r($errors, true) ."


\n\n"; else echo "

OK:[". $table['table'] ."]



\n\n"; } ?>