forked from agmip/ace-dssat-import
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbConnect.php
104 lines (78 loc) · 2.46 KB
/
dbConnect.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
// Define DB connection parameters
function getDBParas() {
$dc_paras["host"]="localhost";
$dc_paras["db"]="ageng_agmip";
$dc_paras["user"]="root";
$dc_paras["pass"]="";
return $dc_paras;
}
// Create DB connection
function connectDB() {
$DBParas = getDBParas();
$dc_host = $DBParas["host"];
$dc_db = $DBParas["db"];
$dc_user = $DBParas["user"];
$dc_pass = $DBParas["pass"];
$con = mysql_connect($dc_host, $dc_user, $dc_pass);
if (!$con) {
die('Could not connect 01: ' . mysql_error());
}
return $con;
}
// run with single SQL statement
function excuteSql($con, $dbConnectInput) {
$DBParas = getDBParas();
$dc_db = $DBParas["db"];
if (!mysql_select_db($dc_db, $con)){
die('Could not connect 02: ' . mysql_error());
}
$dc_sql = str_ireplace("'null'", "null", $dbConnectInput["dc_sql"]);
$result = mysql_query($dc_sql);
if ($dbConnectInput["dc_sql_type"] == "select") {
$dbConnectOutput["dc_process_name"] = $dbConnectInput["dc_process_name"];
$dbConnectOutput["dc_result_num"] = mysql_num_rows($result);
if (mysql_num_rows($result) > 0) {
$j = 0;
while ($row = mysql_fetch_array($result)) {
$dc_result[$j] = $row;
$j++;
}
$dbConnectOutput["dc_result"] = $dc_result;
} else {
$dbConnectOutput["dc_result"] = 0;
}
} else {
$dbConnectOutput["dc_result"] = $result;
$dbConnectOutput["dc_sql"] = $dbConnectInput["dc_sql"];
$dbConnectOutput["dc_errMsg"] = mysql_error();
}
return $dbConnectOutput;
}
// run with mulitple SQL statement
function excuteSqls($con, $dbConnectInput) {
$DBParas = getDBParas();
$dc_db = $DBParas["db"];
if (!mysql_select_db($dc_db, $con)){
die('Could not connect 02: ' . mysql_error());
}
for ($i=0; $i <count($dbConnectInput); $i++) {
$dc_sql = $dbConnectInput[$i]["dc_sql"];
$result = mysql_query($dc_sql);
if ($dbConnectInput[$i]["dc_sql_type"] == "select") {
$dbConnectOutput[$i]["dc_process_name"] = $dbConnectInput[$i]["dc_process_name"];
$dbConnectOutput[$i]["dc_result_num"] = mysql_num_rows($result);
$j = 0;
while ($row = mysql_fetch_array($result)) {
$dc_result[$j] = $row;
$j++;
}
$dbConnectOutput[$i]["dc_result"] = $dc_result;
} else {
$dbConnectOutput[$i]["dc_result"] = $result;
$dbConnectOutput[$i]["dc_sql"] = $dbConnectInput[$i]["dc_sql"];
$dbConnectOutput[$i]["dc_errMsg"] = mysql_error();
}
}
}
?>