38960-vm/api/patients.php
2026-03-21 07:52:14 +00:00

31 lines
884 B
PHP

<?php
require_once __DIR__ . '/../includes/common_data.php';
header('Content-Type: application/json');
$action = $_GET['action'] ?? '';
$pdo = db();
try {
switch ($action) {
case 'search':
$q = $_GET['q'] ?? '';
if (strlen($q) < 1) {
echo json_encode([]);
exit;
}
$stmt = $pdo->prepare("SELECT id, name, phone FROM patients WHERE name LIKE ? OR phone LIKE ? LIMIT 20");
$term = "%$q%";
$stmt->execute([$term, $term]);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($results);
break;
default:
http_response_code(400);
echo json_encode(['error' => 'Invalid action']);
}
} catch (Exception $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage()]);
}