/
dbHandler.php
executable file
·54 lines (50 loc) · 1.19 KB
/
dbHandler.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
<?php
function connectToDb()
{
// Test Database Info
//$mysqli=mysqli_connect("localhost", "root", "root", "elede");
// Actual Database info
$mysqli=mysqli_connect("host", "user", "pass", "table_name");
// Check connection
if (mysqli_connect_errno()) {
return "Failed to connect to MySQL: " . mysqli_connect_error();
} else
{
return $mysqli;
}
}
function closeDb($stmt, $mysqli)
{
$stmt->close();
mysqli_close($mysqli);
}
function getContent() {
$mysqli = connectToDb();
$stmt = $mysqli->prepare("SELECT id, content FROM tips ORDER BY RAND() LIMIT 1");
$stmt->execute();
$stmt->bind_result($iId, $sContent);
$aResult = array();
while($stmt->fetch())
{
$tip = array(
"id" => $iId,
"content" => utf8_encode($sContent)
);
$aResult["tip"] = $tip;
}
$stmt->close();
$stmt = $mysqli->prepare("SELECT id, name, url FROM orgs ORDER BY RAND() LIMIT 1");
$stmt->execute();
$stmt->bind_result($iOid, $sOname, $sOurl);
while($stmt->fetch())
{
$org = array(
"id" => $iOid,
"name" => utf8_encode($sOname),
"url" => $sOurl
);
$aResult["org"] = $org;
}
closeDb($stmt, $mysqli);
return json_encode($aResult);
}