<?php
/**
* Database Connection Debug Script
* Use this to test and debug database connection issues
*/
// Start output buffering
ob_start();
echo "<h2>Database Connection Debug</h2>";
try {
require_once 'config/database.php';
echo "<p>Testing database connection...</p>";
$database = Database::getInstance();
$db = $database->getConnection();
if ($db) {
echo "<p style='color: green;'>✅ Database connection successful!</p>";
// Test a simple query
$stmt = $db->prepare("SELECT COUNT(*) as user_count FROM users");
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
echo "<p>📊 Total users: " . $result['user_count'] . "</p>";
// Check MySQL connection limits
$stmt = $db->prepare("SHOW VARIABLES LIKE 'max_connections'");
$stmt->execute();
$max_conn = $stmt->fetch(PDO::FETCH_ASSOC);
echo "<p>🔧 Max connections: " . $max_conn['Value'] . "</p>";
// Check current connections
$stmt = $db->prepare("SHOW STATUS LIKE 'Threads_connected'");
$stmt->execute();
$current_conn = $stmt->fetch(PDO::FETCH_ASSOC);
echo "<p>🔗 Current connections: " . $current_conn['Value'] . "</p>";
// Check process list
$stmt = $db->prepare("SHOW PROCESSLIST");
$stmt->execute();
$processes = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo "<p>📋 Active processes: " . count($processes) . "</p>";
// Close connection
$database->closeConnection();
echo "<p style='color: green;'>✅ Connection closed successfully!</p>";
} else {
echo "<p style='color: red;'>❌ Database connection failed!</p>";
}
} catch (Exception $e) {
echo "<p style='color: red;'>❌ Error: " . $e->getMessage() . "</p>";
}
echo "<hr>";
echo "<h3>Connection Troubleshooting:</h3>";
echo "<ul>";
echo "<li>Check MySQL service is running</li>";
echo "<li>Verify database credentials</li>";
echo "<li>Check max_connections setting</li>";
echo "<li>Look for long-running queries</li>";
echo "<li>Restart MySQL service if needed</li>";
echo "</ul>";
echo "<p><a href='home.php'>← Back to Home</a></p>";
// Clean up output buffer
ob_end_flush();
?>