-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_latest_block_header.php
47 lines (40 loc) · 1.58 KB
/
get_latest_block_header.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
<?php
// Include database configuration
include 'config_1.php';
// Function to fetch the latest block header and previous row from the database
function getLatestBlockHeaderAndPreviousRow($conn) {
// Query to get the last two rows from the blocks table
$query = "SELECT * FROM blocks ORDER BY id DESC LIMIT 2"; // Adjust table/column names as needed
$result = $conn->query($query);
if ($result && $result->num_rows > 0) {
$rows = $result->fetch_all(MYSQLI_ASSOC);
// If we have at least two rows, treat the second row as the previous row and first as the latest
if (count($rows) == 2) {
return [
'latestBlockHeader' => $rows[0], // Latest block
'previousRow' => $rows[1] // Previous row
];
} elseif (count($rows) == 1) {
// Only one row exists, treat it as both latest and previous (if applicable)
return [
'latestBlockHeader' => $rows[0],
'previousRow' => null // No previous row available
];
}
}
return null; // Return null if no rows found
}
// Main execution
$conn = getDbConnection();
$data = getLatestBlockHeaderAndPreviousRow($conn);
if ($data !== null) {
echo json_encode([
'latestBlockHeader' => $data['latestBlockHeader'],
'previousRow' => $data['previousRow']
]); // Return as JSON response
} else {
http_response_code(404); // Not found response code if no headers are found
echo json_encode(['error' => 'No block headers found.']);
}
$conn->close();
?>