Create table into MySQL db using Php script dynamically

Add tables inside existing MySQL database with use of Php sql query coding on button click.

Building tables using php coding script is very easy because all you have to do is define one time php script code to create table into database then all you have to do is run php file and tables will automatically create inside your existing database. This type of functionality basically used where developer need to make so much tables in their projects and they cannot done the basic table creation procedure because it takes too much time to create. So here is the complete step by step tutorial for Create table into MySQL db using Php script dynamically.

android-project-download-code-button

How to Create table into MySQL db using Php script dynamically.

Firstly create database in phpmyadmin because this script is used to create tables inside existing MySQL database. 

<html>
<head> 
<title>Php Script for Creating Tables inside MySQL db</title>
</head> 
<body>

 <h2>Click on button for Creating Tables inside MySQL db dynamically</h2>
<form method="post" action="">
<input type="submit" name="submit" Value="submit">
</form>
 
</body>
</html>

<?php
if (isset($_POST['submit']))
{

$databasehost = 'localhost';
$databaseuser = 'root';
$databasepass = '';
$conn = mysql_connect($databasehost, $databaseuser, $databasepass);
if(! $conn )
{
 die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully to MySQL <br />';
$sql = "CREATE TABLE Class( ".
 "Class_id INT NOT NULL AUTO_INCREMENT, ".
 "Class_title VARCHAR(100) NOT NULL, ".
 "Class_author VARCHAR(40) NOT NULL, ".
 "Class_date DATE, ".
 "PRIMARY KEY ( Class_id )); ";
 
 //define your existing database name here.
mysql_select_db( 'testdb' );
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
 die('Could not create table: ' . mysql_error());
}
echo "Table created successfully\n";
mysql_close($conn);
 
}

?>

 

Copy your file inside  XAMPP -> htdocs -> project_name_folder -> table_create.php

Start Xampp control panel then start Apache & MySQL .

Open your web browser the type localhost/sample_project/table_create.php .

Screenshot:

dynamic table

Click on Submit button to create tables.

Create table into MySQL db using Php script dynamically

Now open Localhost/phpmyadmin and check your database for conformation.

table created

Here you go now your table is successfully create into your existing database.

Click Here to Download Create table into MySQL db using Php script dynamically source code file.

Leave a Reply

Your email address will not be published. Required fields are marked *