Dan K
2019-08-06 308b57b7a031c2dc7575e87eefda67c1852c6f06
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
<?php
    $link = mysqli_connect($_ENV["DATABASE_SERVICE_NAME"],$_ENV["DATABASE_USER"],$_ENV["DATABASE_PASSWORD"],$_ENV["DATABASE_NAME"]);
    if (!$link) {
        http_response_code (500);
        error_log ("Error: unable to connect to database\n");
    die();
    }
 
    $query = "SELECT count(*) FROM quote";
    $result = $link->query($query);
    if (!$result) {
        http_response_code (500);
        error_log ("SQL error: " . mysqli_error($link) . "\n");
    die();
    }
 
    $row = mysqli_fetch_array($result);
    mysqli_free_result($result);
 
    $id = rand(1,$row[0]);
 
    $query = "SELECT msg FROM quote WHERE id = " . $id;
    $result = $link->query($query);
    if (!$result) {
        http_response_code (500);
        error_log ("SQL error: " . mysqli_error($link) . "\n");
    die();
    }
 
    $row = mysqli_fetch_array($result);
    mysqli_free_result($result);
 
    print $row[0] . "\n";
    
    mysqli_close($link);
?>