Home > AI > Language > PHP > mysqli >

create a table

Working code:

<?php

$db_host = "";
$db_user = "";
$db_password = "";
$db_dbname = "";


// Create connection
$con = new mysqli($db_host, $db_user, $db_password, $db_dbname);
// Check connection
if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
}




// sql to create table
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)";

if ($con->query($sql) === TRUE) {
    echo "Table MyGuests created successfully";
} else {
    echo "Error creating table: " . $con->error;
}





$con->close();

Leave a Reply