db_lib   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 35
rs 10
c 1
b 0
f 0
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A prepared_execute() 0 18 5
A bind_params() 0 13 5
1
<?php
2
3
/**
4
 * Database library, common DB helpers
5
 */
6
class db_lib extends Library {
7
8
    private function bind_params($stmt, $datatypes, $data) {
9
        if (!empty($datatypes) && !empty($data)) {
10
            // Convert to references
11
            $data = array_merge([$datatypes], $data);
12
            foreach ($data as $key => $value) {
13
                $data[$key] = &$data[$key];
14
            }
15
16
            if (!call_user_func_array(array($stmt, "bind_param"), $data)) {
17
                return false;
18
            }
19
        }
20
        return true;
21
    }
22
23
    public function prepared_execute($conn, $sql, $datatypes, $data, $return_stmt = true) {
24
        $stmt = $conn->prepare($sql);
25
        if (!$stmt) {
26
            return false;
27
        }
28
29
        if ($this->bind_params($stmt, $datatypes, $data) === false) {
30
            return false;
31
        };
32
33
        if (!$stmt->execute()) {
34
            return false;
35
        }
36
37
        if ($return_stmt) {
38
            return $stmt;
39
        }
40
        return true;
41
    }
42
43
}
44