-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_stats.php
More file actions
47 lines (41 loc) · 1.2 KB
/
Copy pathfetch_stats.php
File metadata and controls
47 lines (41 loc) · 1.2 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
<?php
require_once 'db.php';
// Initialize response array
$stats = array(
'productCount' => 0,
'orderCount' => 0,
'userCount' => 0
);
try {
// Get total number of products
$query = "SELECT COUNT(*) as count FROM products";
$result = $conn->query($query);
if ($result) {
$row = $result->fetch_assoc();
$stats['productCount'] = intval($row['count']);
}
// Get total number of orders
$query = "SELECT COUNT(*) as count FROM orders";
$result = $conn->query($query);
if ($result) {
$row = $result->fetch_assoc();
$stats['orderCount'] = intval($row['count']);
}
// Get total number of active users
$query = "SELECT COUNT(*) as count FROM users WHERE status = 'active'";
$result = $conn->query($query);
if ($result) {
$row = $result->fetch_assoc();
$stats['userCount'] = intval($row['count']);
}
// Set response headers
header('Content-Type: application/json');
echo json_encode($stats);
} catch (Exception $e) {
// Set error response
http_response_code(500);
echo json_encode(['error' => 'Failed to fetch statistics']);
} finally {
// Close database connection
$conn->close();
}