Crear Tabla en Base de Datos con PHP

PHP



$dbhost = 'localhost';
$dbuser = ''; // tu usuario de sql
$dbpass = ''; // tu password de sql
$dbname = ''; // tu base de datos
$newtable = 'example001'; /nombre de la nueva tabla

$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// SQL to create the countries table
$createTableSQL = "
CREATE TABLE ".$newtable." (
    `Name` varchar(9),
    `Capital` varchar(10),
    `Continent` varchar(7)
)
";

if ($conn->query($createTableSQL) === TRUE) {
    echo "Table '".$newtable."' created successfully
"; // SQL to insert sample data $insertDataSQL = " INSERT INTO ".$newtable." (`Name`, `Capital`, `Continent`) VALUES ('Mexico', 'DF', 'America'), ('USA', 'Washington', 'America'), ('Spain', 'Madrid', 'Europe'), ('France', 'Paris', 'Europe'), ('Egypt', 'Cairo', 'Africa'), ('Portugal', 'Lisboa', 'Europe'), ('Eslovenia', 'Liubliana', 'Europe'), ('Letonia', 'Riga', 'Europe'), ('Italia', 'Roma', 'Europe'), ('Thailand', 'Bangkok', 'Asia'), ('Indonesia', 'Yakarta', 'Asia') "; if ($conn->query($insertDataSQL) === TRUE) { echo "Sample data inserted successfully"; } else { echo "Error inserting data: " . $conn->error; } } else { echo "Error creating table: " . $conn->error; } // Close connection $conn->close();