-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.php
51 lines (44 loc) · 1.3 KB
/
data.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
<?php
header('Content-Type: application/json; charset=utf-8');
// Database configuration
$databaseConfig = [
'host' => 'localhost',
'user' => 'quotout',
'password' => 'qu0t_',
'database' => 'citation'
];
try {
// Create connection
$conn = new mysqli($databaseConfig['host'], $databaseConfig['user'], $databaseConfig['password'], $databaseConfig['database']);
// Check connection
if ($conn->connect_error) {
throw new Exception('Connection failed: ' . $conn->connect_error);
}
// Fetch random quote
$response = fetchRandomQuote($conn);
// Close connection
$conn->close();
} catch (Exception $e) {
$response = [
'quote' => 'Es gab ein Problem bei der Verbindung zur Datenbank. Bitte versuchen Sie es später erneut.',
'author' => $e->getMessage()
];
}
echo json_encode($response);
function fetchRandomQuote($conn)
{
$stmt = $conn->prepare("SELECT * FROM citation ORDER BY RAND() LIMIT 1");
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
return [
'ID' => $row['ID'],
'quote' => $row['quote'],
'author' => $row['first_name'] . ' ' . $row['last_name']
];
} else {
return ['error' => -1];
}
}
?>