What are the best practices for generating multiple tables with different specifications in PHP?

When generating multiple tables with different specifications in PHP, it is best to use a loop to iterate through the different specifications and create each table dynamically. This allows for a more scalable and maintainable solution.

<?php
// Define an array of table specifications
$tables = [
    ['name' => 'users', 'columns' => ['id INT AUTO_INCREMENT', 'name VARCHAR(50)', 'email VARCHAR(50)']],
    ['name' => 'products', 'columns' => ['id INT AUTO_INCREMENT', 'name VARCHAR(50)', 'price DECIMAL(10,2)']]
];

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

$conn = new mysqli($servername, $username, $password, $dbname);

// Create tables dynamically
foreach ($tables as $table) {
    $sql = "CREATE TABLE " . $table['name'] . " (";
    $sql .= implode(", ", $table['columns']);
    $sql .= ")";

    if ($conn->query($sql) === TRUE) {
        echo "Table " . $table['name'] . " created successfully<br>";
    } else {
        echo "Error creating table: " . $conn->error . "<br>";
    }
}

// Close the database connection
$conn->close();
?>