';
}
if (!$is_connected) {
// Connection Form - Dark Theme
echo '
';
} else {
// Database is connected, show tabs and content
$current_db = $_GET['db'] ?? 'overview';
echo '';
// Overview Tab
if ($current_db === 'overview') {
try {
// Get tables with detailed information
if (!empty($conn['name']) || $conn['type'] === 'sqlite') {
if ($conn['type'] === 'mysql') {
$stmt = $pdo->query("SHOW TABLE STATUS");
$tables = $stmt->fetchAll(PDO::FETCH_ASSOC);
} elseif ($conn['type'] === 'sqlite') {
$stmt = $pdo->query("SELECT name FROM sqlite_master WHERE type='table'");
$table_names = $stmt->fetchAll(PDO::FETCH_COLUMN);
$tables = [];
foreach ($table_names as $tname) {
$tables[] = ['Name' => $tname];
}
}
if (count($tables) > 0) {
echo '
Tables and views
';
echo '
Search tables in database:
';
echo '
';
echo '
';
echo '
Selected: ';
echo ' ';
echo ' ';
echo ' ';
echo '';
echo '
';
} else {
echo '
No tables found.
';
}
} else {
// Show database list if no database selected
if ($conn['type'] === 'mysql') {
$stmt = $pdo->query("SHOW DATABASES");
$databases = $stmt->fetchAll(PDO::FETCH_COLUMN);
echo '
Databases
';
echo '
';
}
}
} catch (PDOException $e) {
echo '
Error: ' . htmlspecialchars($e->getMessage()) . '
';
}
}
// SQL Query Tab
elseif ($current_db === 'sql') {
if (isset($_POST['execute_sql'])) {
$sql = $_POST['sql_query'];
try {
$start_time = microtime(true);
$stmt = $pdo->query($sql);
$execution_time = round((microtime(true) - $start_time) * 1000, 2);
// Check if it's a SELECT query
if ($stmt->columnCount() > 0) {
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo '
Query executed OK, ' . count($results) . ' rows affected (' . $execution_time . ' ms)
';
if (count($results) > 0) {
echo '
';
echo '';
foreach (array_keys($results[0]) as $column) {
echo '| ' . htmlspecialchars($column) . ' | ';
}
echo '
';
foreach ($results as $row) {
echo '';
foreach ($row as $value) {
echo '| ' . htmlspecialchars($value ?? 'NULL') . ' | ';
}
echo '
';
}
echo '
';
}
} else {
$affected = $stmt->rowCount();
echo '
Query executed OK, ' . $affected . ' rows affected (' . $execution_time . ' ms)
';
}
} catch (PDOException $e) {
echo '
Error in query: ' . htmlspecialchars($e->getMessage()) . '
';
}
}
echo '
';
}
// Tables List Tab
elseif ($current_db === 'tables') {
try {
if ($conn['type'] === 'mysql') {
$stmt = $pdo->query("SHOW TABLES");
} elseif ($conn['type'] === 'sqlite') {
$stmt = $pdo->query("SELECT name FROM sqlite_master WHERE type='table'");
} elseif ($conn['type'] === 'pgsql') {
$stmt = $pdo->query("SELECT tablename FROM pg_tables WHERE schemaname='public'");
}
$tables = $stmt->fetchAll(PDO::FETCH_COLUMN);
echo '
Database Tables (' . count($tables) . ')
';
echo '
';
} catch (PDOException $e) {
echo '
Error: ' . htmlspecialchars($e->getMessage()) . '
';
}
}
// View Table Data
elseif ($current_db === 'table' && isset($_GET['tablename'])) {
$table = $_GET['tablename'];
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$per_page = 50;
$offset = ($page - 1) * $per_page;
try {
// Get primary key
$primary_key = null;
if ($conn['type'] === 'mysql') {
$pk_stmt = $pdo->query("SHOW KEYS FROM `$table` WHERE Key_name = 'PRIMARY'");
$pk_result = $pk_stmt->fetch(PDO::FETCH_ASSOC);
if ($pk_result) {
$primary_key = $pk_result['Column_name'];
}
} elseif ($conn['type'] === 'sqlite') {
$pk_stmt = $pdo->query("PRAGMA table_info(`$table`)");
$columns = $pk_stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($columns as $col) {
if ($col['pk'] == 1) {
$primary_key = $col['name'];
break;
}
}
}
// Get total count
$count_stmt = $pdo->query("SELECT COUNT(*) FROM `$table`");
$total_rows = $count_stmt->fetchColumn();
$total_pages = ceil($total_rows / $per_page);
// Get data
$stmt = $pdo->query("SELECT * FROM `$table` LIMIT $per_page OFFSET $offset");
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo '
';
if (count($results) > 0) {
// Info bar
echo '
';
echo 'Showing ' . ($offset + 1) . '-' . min($offset + $per_page, $total_rows) . ' of ' . number_format($total_rows) . ' rows';
echo '
';
// Table
echo '
';
echo '
';
echo '';
foreach (array_keys($results[0]) as $column) {
echo '| ' . htmlspecialchars($column) . ' | ';
}
echo 'Actions | ';
echo '
';
foreach ($results as $row) {
echo '';
foreach ($row as $key => $value) {
$display_value = htmlspecialchars($value ?? 'NULL');
// Truncate but show in tooltip
if (strlen($display_value) > 80) {
$short = substr($display_value, 0, 80) . '...';
echo '' . $short . ' | ';
} else {
echo '' . $display_value . ' | ';
}
}
echo '';
if ($primary_key && isset($row[$primary_key])) {
echo '';
echo ' ';
echo '';
echo '';
} else {
echo 'No PK';
}
echo ' | ';
echo '
';
}
echo '
';
echo '
';
// Pagination
if ($total_pages > 1) {
echo '
';
if ($page > 1) {
echo '
';
echo ' Previous';
}
echo '
Page ' . $page . ' of ' . $total_pages . '';
if ($page < $total_pages) {
echo '
';
echo 'Next ';
}
echo '
';
}
} else {
echo '
No records found in this table.
';
}
} catch (PDOException $e) {
echo '
Error: ' . htmlspecialchars($e->getMessage()) . '
';
}
}
// Edit Record
elseif ($current_db === 'edit' && isset($_GET['tablename']) && isset($_GET['id'])) {
$table = $_GET['tablename'];
$pk = $_GET['pk'];
$id = $_GET['id'];
try {
// Handle form submission
if (isset($_POST['update_record'])) {
$updates = [];
$params = [];
foreach ($_POST as $key => $value) {
if ($key !== 'update_record') {
$updates[] = "`$key` = ?";
$params[] = $value;
}
}
$params[] = $id;
$sql = "UPDATE `$table` SET " . implode(', ', $updates) . " WHERE `$pk` = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
echo "";
}
// Get current record
$stmt = $pdo->prepare("SELECT * FROM `$table` WHERE `$pk` = ?");
$stmt->execute([$id]);
$record = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$record) {
echo '
Record not found!
';
} else {
echo '
Edit Record in Table: ' . htmlspecialchars($table) . '
';
echo '
';
}
} catch (PDOException $e) {
echo '
Error: ' . htmlspecialchars($e->getMessage()) . '
';
}
}
// Insert Record
elseif ($current_db === 'insert' && isset($_GET['tablename'])) {
$table = $_GET['tablename'];
try {
// Handle form submission
if (isset($_POST['insert_record'])) {
$columns = [];
$values = [];
$params = [];
foreach ($_POST as $key => $value) {
if ($key !== 'insert_record' && $value !== '') {
$columns[] = "`$key`";
$values[] = "?";
$params[] = $value;
}
}
if (count($columns) > 0) {
$sql = "INSERT INTO `$table` (" . implode(', ', $columns) . ") VALUES (" . implode(', ', $values) . ")";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
echo "";
}
}
// Get table structure
if ($conn['type'] === 'mysql') {
$stmt = $pdo->query("DESCRIBE `$table`");
$columns = $stmt->fetchAll(PDO::FETCH_ASSOC);
} elseif ($conn['type'] === 'sqlite') {
$stmt = $pdo->query("PRAGMA table_info(`$table`)");
$columns = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
echo '
Insert New Record into Table: ' . htmlspecialchars($table) . '
';
echo '
';
} catch (PDOException $e) {
echo '
Error: ' . htmlspecialchars($e->getMessage()) . '
';
}
}
// Delete Record
elseif ($current_db === 'delete' && isset($_GET['tablename']) && isset($_GET['id'])) {
$table = $_GET['tablename'];
$pk = $_GET['pk'];
$id = $_GET['id'];
try {
$stmt = $pdo->prepare("DELETE FROM `$table` WHERE `$pk` = ?");
$stmt->execute([$id]);
echo "";
} catch (PDOException $e) {
echo '
Error deleting record: ' . htmlspecialchars($e->getMessage()) . '
';
echo '
Back to Table';
}
}
// View Table Structure
elseif ($current_db === 'structure' && isset($_GET['tablename'])) {
$table = $_GET['tablename'];
try {
if ($conn['type'] === 'mysql') {
$stmt = $pdo->query("DESCRIBE `$table`");
} elseif ($conn['type'] === 'sqlite') {
$stmt = $pdo->query("PRAGMA table_info(`$table`)");
} elseif ($conn['type'] === 'pgsql') {
$stmt = $pdo->query("SELECT column_name, data_type, is_nullable FROM information_schema.columns WHERE table_name = '$table'");
}
$columns = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo '
Table Structure: ' . htmlspecialchars($table) . '
';
echo '
';
echo '';
foreach (array_keys($columns[0]) as $col) {
echo '| ' . htmlspecialchars($col) . ' | ';
}
echo '
';
foreach ($columns as $column) {
echo '';
foreach ($column as $value) {
echo '| ' . htmlspecialchars($value ?? 'NULL') . ' | ';
}
echo '
';
}
echo '
';
} catch (PDOException $e) {
echo '
Error: ' . htmlspecialchars($e->getMessage()) . '
';
}
}
// Select Database
elseif ($current_db === 'selectdb' && isset($_GET['dbname'])) {
$dbname = $_GET['dbname'];
$_SESSION['db_connection']['name'] = $dbname;
echo "";
}
}
echo '