Creating table in PHP – With Program

This blog explains about Creating table in PHP – With Program . It is explained clearly well below :


Creating Tables inside MySQL Database Using PHP

In the previous chapter we’ve learned how to create a database on MySQL server. Now it’s time to create some tables inside the database that will actually hold the data. A table organizes the information into rows and columns.

The SQL CREATE TABLE statement is used to create a table in database.

Let’s make a SQL query using the CREATE TABLE statement, after that we will execute this SQL query through passing it to the PHP mysqli_query() function to finally create our table.

We have created a database “trainee” in last program. Now we are going to create a table inside the database “trainee”. I am assume a table name is details and columns are Name, Father and Mobile, all are having the length 20. Ok Let us go to the program,

/* Create New Table */
$host = “localhost”;
$user = “root”;
$pass = “”;
$connect=mysql_connect($host, $user, $pass);
mysql_select_db(“trainee”); // Select your created database to create new table
$query=”create table Details(Name varchar(20), Father varchar(20), Mobile varchar(20))”;
echo “New Table is created successfully”;


New Table is created successfully

The PHP code in the above example creates a table named persons with four columns idfirst_namelast_name and email inside the demo database.

Notice that each field name is followed by a data type declaration; this declaration specifies what type of data the column can hold, whether integer, string, date, etc.

There are a few additional constraints (also called modifiers) that are specified after the column name in the preceding SQL statement, like NOT NULLPRIMARY KEYAUTO_INCREMENT, etc. Constraints define rules regarding the values allowed in columns.

Please check out the tutorial on SQL CREATE TABLE statement for the detailed information about syntax, as well as the data types and constraints available in MySQL database system.