Creating a Table
In this lesson we are going to write a PHP script that will create a table in our MySQL database.
To create a table in MySQL, use the command CREATE TABLE tablename. In PHP,mysql_query() function is used to execute CREATE TABLE command and other SQL query.
Here is an example script:
<?php//including the database connection file include("mysql_connect.php");// Create tablemysql_select_db("employee", $con); $sql = "CREATE TABLE Persons ( id int unsigned not null auto_increment primary key, f_name varchar(20), l_name varchar(20), position varchar(30), age int, salary int, email varchar(60) )";// Execute query mysql_query($sql); ?>
In the above code we have created a employee_record table in our employee database. The table consists of id, l_name, f_name, position, age, salary and email.
Note: Select first the database where you want to create your table. The database is selected with the mysql_select_db() function.