Create New mySQL Table using PHP
Create New mySQL Table using PHP.In this article i will step through how to create a new MySQL table using PHP.
I will create a new table call ” mytable1 “ in “janedb “ Database
Prior to that you need to complete the items listed Below
- Mamp or xampp with phpMyAdmin install into your Computer , check out here
- New User Created . Check out Here
- Create a new MySQL data base Here
Step 1
Start your My SQL and Apache Server
Step 2
Create a new Script
(1) First to connect to the Database
(2) Create anew MySQL Table call “mytable 1”inside “janedb “ Database
<?php /* Declare User connecting Credential*/ $hostName ="localhost"; $userName ="jane"; $userPassword ="jane"; $database ="janedb"; /* return connection status through $dbConnectionStatus Boolean Variable*/ /* using mysqli Object*/ $dbConnectionStatus = new mysqli($hostName, $userName, $userPassword,$database); /* Check whether the User Succesfully connect to localhost and mydata Database*/ /* -> Access the instance "$dbConnectionStatus" variable property "connect_error " boolean */ if ($dbConnectionStatus->connect_error){ die("Connection failed: " . $dbConnectionStatus->connect_error); } $sql = "CREATE TABLE mytable1 ( 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 )"; $createNewTable = $dbConnectionStatus -> query($sql); if ($createNewTable) { echo "Table myTable created "; } else { echo "Error to create table: " . $dbConnectionStatus->error; } $dbConnectionStatus->close(); ?>
Step 3
Save this File as “.php” extension in
C:\xampp\htdocs
If you want to change the root Location of the mySQL file check out this article at step 3
Step 4
Test it
(1)Click Admin
(2) Click the PHP File you have just created in this example i name it ” createTable.php”
Leave a Reply