<?php
// Find Database Names
error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "<h2>Database Discovery</h2>";

// Use the same config as your system
require_once 'config.php';

// Check what variable we have
echo "<h3>1. Available Database Variables</h3>";
if (isset($conn)) {
    echo "<p style='color: green;'>✅ Found '\$conn' variable (object)</p>";
    echo "<p>Connection type: " . gettype($conn) . "</p>";
    
    if ($conn instanceof mysqli) {
        echo "<p>Connection object: MySQLi</p>";
        
        // List all databases
        $result = $conn->query("SHOW DATABASES");
        if ($result) {
            echo "<h3>2. Available Databases</h3>";
            echo "<ul>";
            while ($row = $result->fetch_array()) {
                $db_name = $row[0];
                echo "<li><strong>$db_name</strong> (click to test connection)</li>";
            }
            echo "</ul>";
        }
        
        // List tables in the current database
        $current_db = $conn->query("SELECT DATABASE()");
        if ($current_db) {
            $row = $current_db->fetch_array();
            $current_database = $row[0];
            echo "<h3>3. Tables in current database: <span style='color: blue;'>$current_database</span></h3>";
            
            $tables = $conn->query("SHOW TABLES");
            if ($tables && $tables->num_rows > 0) {
                echo "<ul>";
                while ($table = $tables->fetch_array()) {
                    echo "<li>" . $table[0] . "</li>";
                }
                echo "</ul>";
            } else {
                echo "<p>No tables found in this database.</p>";
            }
        }
        
    }
} else {
    echo "<p style='color: red;'>❌ No \$conn variable found</p>";
}

// Also check for other common variable names
$common_names = ['connection', 'db', 'database', 'mysqli'];
echo "<h3>4. Checking for other connection variables</h3>";
foreach ($common_names as $var_name) {
    if (isset($$var_name)) {
        echo "<p style='color: green;'>✅ Found \$$var_name variable</p>";
    }
}

// Check global scope
global $conn;
if (isset($conn) && $conn instanceof mysqli) {
    echo "<h3>5. Global Connection Test</h3>";
    echo "<p style='color: green;'>✅ Global \$conn is available and connected</p>";
    echo "<p>Current database: " . $conn->query("SELECT DATABASE()")->fetch_array()[0] . "</p>";
}
?>