-
Notifications
You must be signed in to change notification settings - Fork 1
/
import-template.php
82 lines (62 loc) · 1.86 KB
/
import-template.php
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
#source db DSN - see README
$mas90db = "DNS=MAS90Mirror;";
#destination db DSN - see README
$mssqldb_dsn = "SQLServer";
$mssqldb_username = "Administrator";
$mssqldb_password = "somegreatpassword";
#sends a notification email after completion
$email_to_notify = "[email protected]";
#name of table to mirror
$table_name = "AR_Customer";
$start_time = time();
error_reporting(E_ALL);
ini_set('display_errors','On');
#helper
function addslashes_mssql($str){
if (is_array($str)) {
foreach($str AS $id => $value) {
$str[$id] = addslashes_mssql($value);
}
} else {
$str = str_replace("'", "''", $str);
}
return $str;
}
#db connects
$db=odbc_connect($mas90db,"","");
$mssql=odbc_connect($mssqldb_dsn,$mssqldb_username,$mssql_password);
// Select all rows from the desired table
$sql="SELECT * FROM $table_name";
$query=odbc_exec($db, $sql);
$p = 0;
#Drop all rows from destination table (ensures data integrity)
$check = "TRUNCATE TABLE $table_name;";
odbc_exec($mssql,$check);
while($row = odbc_fetch_array($query))
{
$p++;
#Clear values
$keys = "";
$values = "";
#Build the query
foreach ($row as $k => $v)
{
$keys .= $k.",";
$values .= "'".addslashes_mssql($v)."',";
}
#Join it all together
$keys = substr($keys,0,strlen($keys)-1);
$values = substr($values,0,strlen($values)-1);
$insert = sprintf("INSERT INTO $table_name (%s) VALUES (%s);",$keys,$values);
odbc_exec($mssql,$insert);
}
$end_time = time();
$elapsed_time = date("i\m, s\s",($end_time-$start_time));;
$to = $email_to_notify;
$subject = "$table_name mirrored succesfully";
$message = "$table_name mirrored successfully in $elapsed_time with $p rows inserted.";
$headers = 'From: MAS90Mirror' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>