PHP Create a Table in a MySQL Database
This PHP script creates a table in a MySQL database.
<?php /** * Create a Table in a MySQL Database * * This script creates a table in a * MySQL database. * * @author PHP Simple * @website http://phpsimple.com * @date August 2021 */ $dc_host = "localhost"; // Host Name $dc_user = "admin"; // Username $dc_pass = "password"; // Password $dc_db = "database"; // Database Name // Create connection $dc_conn = new mysqli($dc_host, $dc_user, $dc_pass, $dc_db); // Check connection if ($dc_conn->connect_error) { die("Connection failed: " . $dc_conn->connect_error); } // Select Records $ct_sql = "CREATE TABLE MyTable ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, firstname VARCHAR(30) NOT NULL, lastname VARCHAR(30) NOT NULL, email VARCHAR(50), reg_date TIMESTAMP )"; // For a list of available datatypes, please see https://www.w3schools.com/sql/sql_datatypes.asp if ($dc_conn->query($ct_sql) === TRUE) { echo "Table was created successfully!"; } else { echo "Error: " . $dc_conn->error; } // Close the connection $dc_conn->close(); ?>