Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.61 KB | None | 0 0
  1. <?php
  2. #Connection variables for MYSQL:
  3. $mysql_host = 'SERVER';
  4. $mysql_user = 'haldur';
  5. $mysql_password = 'PAROOL';
  6. $mysql_database = 'test';
  7. $mysql_link = new PDO(
  8.     "mysqli:host=$mysql_host;dbname=$mysql_database;charset=utf8",
  9.     $mysql_user,
  10.     $mysql_password
  11. );
  12.  
  13. #Connection variables for MSSSQL:
  14. $mssql_host = '213.168.13.18:1433';
  15. $mssql_user = 'VAATAJA';
  16. $mssql_password = 'PAROOL';
  17. $mssql_database = 'BAAS';
  18. $mssql_link = new PDO(
  19.     "dblib:host=$mssql_host;dbname=$mssql_database",
  20.     $mssql_user,
  21.     $mssql_password
  22. );
  23.  
  24. $tables = array('TABEL'); # tabeli nimed siia
  25.  
  26. #Select the databases:
  27.  
  28. #SET CHARACTER SET to utf8:
  29. $mysql_link->prepare("SET NAMES 'utf8'")->execute();
  30. $mysql_link->prepare("SET CHARACTER SET 'utf8'")->execute();
  31.  
  32. #Delete all records on MYSQL tabel:
  33. $deleteQuery = "DELETE FROM TABEL";
  34.  
  35. #Migrate the data:
  36. foreach($tables as $table){
  37.   $deleteQuery = "DELETE FROM $table";
  38.   try{
  39.         $mysql_link->prepare($deleteQuery)->execute();
  40.     }
  41.     catch (PDOException $e){
  42.         echo $e->getMessage();
  43.     }
  44.   $m_res = $mssql_link->prepare('select * from ?')->execute($table);
  45.  
  46.   $j = 0;
  47.  
  48.   while($rec = $m_res->fetch(PDO::FETCH_NUM)){
  49.       echo $table, ' >> ', $j++, "\n";
  50.       $cols = count($rec);
  51.       for($i = 0; $i < $cols; $i++){
  52.           if(is_string($rec[$i])){
  53.               $rec[$i] = $mysql_link->quote($rec[$i]);
  54.           }
  55.           if(is_null($rec[$i])) $rec[$i] = 'NULL';
  56.       }
  57.       $query .= "(" . implode(",", $rec) . "),";
  58.   }
  59.   $sql = "INSERT INTO ? VALUES ". $query;
  60.   try{
  61.     $mysql_link->prepare($sql)->execute($table);
  62.   }
  63.   catch (PDOException $e)
  64.   {
  65.     echo $e->getMessage();
  66.   }
  67. }
  68. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement