check_database.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. header("Content-Type: application/json; charset=UTF-8");
  3. header("Access-Control-Allow-Origin: *");
  4. header("Access-Control-Allow-Methods: GET, OPTIONS");
  5. header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  6. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  7. exit(0);
  8. }
  9. require_once '../config/database.php';
  10. $database = new Database();
  11. $conn = null;
  12. try {
  13. $conn = $database->getConnection();
  14. // Check database connection
  15. $tables = [];
  16. $result = $conn->query("SHOW TABLES");
  17. while ($row = $result->fetch(PDO::FETCH_NUM)) {
  18. $tables[] = $row[0];
  19. }
  20. // Check for key tables
  21. $keyTables = ['users', 'items', 'clients', 'chart_of_accounts'];
  22. $missingTables = array_diff($keyTables, $tables);
  23. // Check for admin user
  24. $adminExists = false;
  25. if (in_array('users', $tables)) {
  26. $checkAdmin = $conn->prepare("SELECT COUNT(*) as count FROM users WHERE username = 'admin'");
  27. $checkAdmin->execute();
  28. $adminCount = $checkAdmin->fetch(PDO::FETCH_ASSOC)['count'];
  29. $adminExists = $adminCount > 0;
  30. }
  31. $isInitialized = empty($missingTables) && $adminExists;
  32. echo json_encode([
  33. "success" => true,
  34. "connected" => true,
  35. "initialized" => $isInitialized,
  36. "tables" => $tables,
  37. "table_count" => count($tables),
  38. "missing_tables" => $missingTables,
  39. "admin_user_exists" => $adminExists,
  40. "status" => $isInitialized ? "ready" : "needs_initialization"
  41. ]);
  42. } catch (Exception $e) {
  43. http_response_code(500);
  44. echo json_encode([
  45. "success" => false,
  46. "connected" => false,
  47. "initialized" => false,
  48. "error" => $e->getMessage(),
  49. "status" => "connection_failed"
  50. ]);
  51. }
  52. ?>