forked from cphlgit/eid_dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport_into_db2.php
45 lines (35 loc) · 891 Bytes
/
import_into_db2.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
<?php
echo "started at ".date("H:i:s")."\n";
$link=mysql_connect('localhost',"root","chai8910");
if(!$link){
die('connection to server failed:' . mysql_error());
}
mysql_select_db("rev",$link) or die(mysql_error());
$res=mysql_query("DESC batches");
$cols=[];
while($row=mysql_fetch_array($res)){
$cols[]="`".$row['Field']."`";
}
$cols_str=implode(",", $cols);
$csv_data = array_map('str_getcsv', file('batches.csv'));
//echo count($csv_data);
insert("batches",$cols_str,$csv_data);
function cleanVals($row){
$ret=[];
foreach ($row as $v) {
$ret[]="'$v'";
}
return $ret;
}
function insert($table,$cols_str,$data){
foreach ($data as $row) {
$row2=cleanVals($row);
$vals_str=implode(",", $row2);
$sql="INSERT INTO `$table` ($cols_str) VALUES ($vals_str)";
if(mysql_query($sql)){
echo "going good\n";
}else{
echo "bad bad bad bad bad bad $sql\n";
}
}
}