-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.php
More file actions
65 lines (54 loc) · 1.8 KB
/
db.php
File metadata and controls
65 lines (54 loc) · 1.8 KB
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
<?php
$dbConfigFileName = "config/config_db.xml";
function getDBParams() {
global $dbConfigFileName;
$xmlData = simplexml_load_file($dbConfigFileName);
$server = $xmlData->server;
$uname = $xmlData->uname;
$pass = $xmlData->pass;
$dbname = $xmlData->dbname;
return array("server"=>$server, "uname"=>$uname, "pass"=>$pass, "dbname"=>$dbname);
}
function connect() {
$dbParams = getDBParams();
// $conn = new mysqli($server, $uname, $pass, $dbname);
$conn = new mysqli($dbParams["server"], $dbParams["uname"], $dbParams["pass"], $dbParams["dbname"]);
return $conn;
}
function closeConnection($conn) {
$conn->close();
}
function execute_and_fetch_assoc($stmt, $types = false, $params = false) {
if (!$stmt->execute()) {
return false;
}
$stmt->store_result();
// get column names
$metadata = $stmt->result_metadata();
$fields = $metadata->fetch_fields();
$results = [];
$ref_results = [];
foreach($fields as $field){
$results[$field->name]=null;
$ref_results[]=&$results[$field->name];
}
call_user_func_array(array($stmt, 'bind_result'), $ref_results);
$data = array();
$i=0;
while ($stmt->fetch()) {
$c = 0;
$new_res = array();
foreach($results as $f => $v) {
$new_res[$f] = $v;
$c++;
}
// echo implode(",", $new_res);
array_push($data, $new_res);
}
// echo count($data);
// foreach($data as $d)
// echo implode(",", $d)."<br/>";
$stmt->free_result();
return $data;
}
?>