32 lines
801 B
PHP
32 lines
801 B
PHP
<?php
|
|
require_once '../db/config.php';
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
$id = $_GET['id'] ?? null;
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
if ($id) {
|
|
$stmt = $pdo->prepare("SELECT * FROM customers WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
$customer = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($customer) {
|
|
echo json_encode($customer);
|
|
} else {
|
|
http_response_code(404);
|
|
echo json_code(['error' => 'Customer not found']);
|
|
}
|
|
} else {
|
|
$stmt = $pdo->query("SELECT * FROM customers");
|
|
$customers = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
echo json_encode($customers);
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['error' => 'Database error: ' . $e->getMessage()]);
|
|
}
|