-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathdb-init.php
More file actions
52 lines (45 loc) · 1.59 KB
/
db-init.php
File metadata and controls
52 lines (45 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
// Include your DB connection here if not already done
require_once '_config.php'; // Make sure this sets up $conn
function importDatabaseIfMissing($conn, $sqlFilePath) {
// List one or more essential tables to check for
$requiredTables = ['users', 'comments'];
// Check if at least one required table exists
$existingTables = [];
$result = $conn->query("SHOW TABLES");
if ($result) {
while ($row = $result->fetch_row()) {
$existingTables[] = $row[0];
}
}
// Check if any required table is missing
$missing = false;
foreach ($requiredTables as $table) {
if (!in_array($table, $existingTables)) {
$missing = true;
break;
}
}
// If missing, import the SQL file
if ($missing) {
if (!file_exists($sqlFilePath)) {
die("SQL file not found: " . htmlspecialchars($sqlFilePath));
}
$sql = file_get_contents($sqlFilePath);
if ($conn->multi_query($sql)) {
do {
// Flush multi_query results
if ($result = $conn->store_result()) {
$result->free();
}
} while ($conn->next_result());
echo "✅ Database imported successfully from {$sqlFilePath}";
} else {
die("❌ Error importing database: " . $conn->error);
}
} else {
echo "✔️ Database already initialized.";
}
}
// Call it (adjust path if needed)
importDatabaseIfMissing($conn, __DIR__ . '/database.sql');