Menu

[f1bb48]: / api / databasecontroller.class.php  Maximize  Restore  History

Download this file

80 lines (64 with data), 2.0 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
class DatabaseController
{
public $dbh;
protected $host;
protected $port;
protected $user;
protected $pw;
protected $db;
var $charset = "utf8mb4";
function __construct($host, $port, $user, $pw, $db) {
$this->host = $host;
$this->port = $port;
$this->user = $user;
$this->pw = $pw;
$this->db = $db;
$this->Connect();
}
function __destruct() {
$this->Disconnect();
}
function Connect() {
$this->dbh = mysqli_connect($this->host, $this->user, $this->pw, $this->db, $this->port);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
return null;
}
if ($this->charset != '') mysqli_set_charset($this->dbh, $this->charset);
return $this->dbh;
}
function Disconnect() {
if (isset($this->dbh)) {
mysqli_close($this->dbh);
$this->dbh = null;
}
}
// run a query that returns maximum one line
function Query($qry, $resulttype = 'a') {
$fetch = false;
if (!isset($this->dbh)) $this->Connect();
$result = mysqli_query($this->dbh, $qry);
if ($result) {
if (mysqli_affected_rows($this->dbh) > 0) {
$fetch = mysqli_fetch_array($result);
}
mysqli_free_result($result);
}
return $fetch;
}
// run a query that returns 0-n lines
function Fetch($qry, $resulttype = 'n') {
$fetch = false;
if (!isset($this->dbh)) $this->Connect();
$result = mysqli_query($this->dbh, $qry);
if ($result) {
if (mysqli_affected_rows($this->dbh) > 0) {
$fetch = array();
while ($row = mysqli_fetch_array($result)) $fetch[] = $row;
}
mysqli_free_result($result);
}
return $fetch;
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.