Db_Mysqli::num_rows()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php
2
class Db_Mysqli implements IDb {
3
    private $link;
4
    private $last_error;
5
6
    public function connect($host, $user, $pass, $db, $port) {
7
        if ($port) {
8
                    $this->link = mysqli_connect($host, $user, $pass, $db, $port);
9
        } else {
10
                    $this->link = mysqli_connect($host, $user, $pass, $db);
11
        }
12
13
        if ($this->link) {
14
            $this->init();
15
16
            return $this->link;
17
        } else {
18
            print("Unable to connect to database (as $user to $host, database $db): ".mysqli_connect_error());
19
            exit(102);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
20
        }
21
    }
22
23
    public function escape_string($s, $strip_tags = true) {
24
        if ($strip_tags) {
25
            $s = strip_tags($s);
26
        }
27
28
        return mysqli_real_escape_string($this->link, $s);
29
    }
30
31
    public function query($query, $die_on_error = true) {
32
        $result = @mysqli_query($this->link, $query);
33
        if (!$result) {
34
            $this->last_error = @mysqli_error($this->link);
35
36
            @mysqli_query($this->link, "ROLLBACK");
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mysqli_query(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

36
            /** @scrutinizer ignore-unhandled */ @mysqli_query($this->link, "ROLLBACK");

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
37
            user_error("query $query failed: ".($this->link ? $this->last_error : "No connection"),
38
                $die_on_error ? E_USER_ERROR : E_USER_WARNING);
39
        }
40
41
        return $result;
42
    }
43
44
    public function fetch_assoc($result) {
45
        return mysqli_fetch_assoc($result);
46
    }
47
48
49
    public function num_rows($result) {
50
        return mysqli_num_rows($result);
51
    }
52
53
    public function fetch_result($result, $row, $param) {
54
        if (mysqli_data_seek($result, $row)) {
55
            $line = mysqli_fetch_assoc($result);
56
            return $line[$param];
57
        } else {
58
            return false;
59
        }
60
    }
61
62
    public function close() {
63
        return mysqli_close($this->link);
64
    }
65
66
    public function affected_rows($result) {
67
        return mysqli_affected_rows($this->link);
68
    }
69
70
    public function last_error() {
71
        return mysqli_error($this->link);
72
    }
73
74
    public function last_query_error() {
75
        return $this->last_error;
76
    }
77
78
    public function init() {
79
        $this->query("SET time_zone = '+0:0'");
80
81
        if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) {
0 ignored issues
show
Bug introduced by
The constant MYSQL_CHARSET was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
82
            mysqli_set_charset($this->link, MYSQL_CHARSET);
83
        }
84
85
        return true;
86
    }
87
88
}
89