<?php
// Database credentials
$host = "localhost";
$user = "scheduling";
$pass = "&xO,yUr5Z_4jBc3D";
$dbname = "scheduling_classroom_reservation"; // change to your DB name
// Connect
$conn = new mysqli($host, $user, $pass, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Set headers for SQL file download
header('Content-Type: application/sql');
header('Content-Disposition: attachment; filename="'.$dbname.'_backup_'.date("Y-m-d_H-i-s").'.sql"');
// Get all tables
$tables = [];
$result = $conn->query("SHOW TABLES");
while ($row = $result->fetch_array()) {
$tables[] = $row[0];
}
$output = "";
// Loop through tables
foreach ($tables as $table) {
$result = $conn->query("SELECT * FROM `$table`");
$num_fields = $result->field_count;
// Table creation statement
$row2 = $conn->query("SHOW CREATE TABLE `$table`")->fetch_row();
$output .= "\n\n" . $row2[1] . ";\n\n";
// Insert data
while ($row = $result->fetch_row()) {
$output .= "INSERT INTO `$table` VALUES(";
for ($j = 0; $j < $num_fields; $j++) {
$row[$j] = addslashes($row[$j]);
$row[$j] = str_replace("\n","\\n",$row[$j]);
$output .= isset($row[$j]) ? '"' . $row[$j] . '"' : '""';
if ($j < ($num_fields - 1)) { $output .= ','; }
}
$output .= ");\n";
}
$output .= "\n\n\n";
}
// Output the SQL dump
echo $output;
$conn->close();
?>