![]() |
|
PHP creating MySQL tables. Free online PHP and MySQL Web Database Programming Tutorial... |
| Lessons | Creating MySQL Tables |
|
|
CREATE TABLE my_table ( column_name1 data_type, column_name2 data_type, column_name3 data_type, .... ) How to Create Tables in PHP? We have to add the CREATE TABLE statement to the mysql_query() function to execute the command and to create a table. Let's see an example: Example The following example creates a table named "my_table", with three columns. The column names being "Name", "Age" and "Address": <?php $con = mysql_connect("localhost", "adam", "ada"); if (!$con) { die('Error connecting database'); } // Create a database if (mysql_query("CREATE DATABASE my_db",$con)) { echo "Database successfully created!"; } else { echo "Error creating database"; } // Create a table mysql_select_db("my_db", $con); $sql = "CREATE TABLE my_table ( Name varchar(25), Age int, Address varchar(75), )"; // Execute query mysql_query($sql, $con); mysql_close($con); ?> Please note that before creating a table we must select an existing database or create a new one. The reason is very obvious a database is a named collection of related tables and a table has to be stored / created inside a database. We select a database with the mysql_select_db() function.
Next: MySQL Primary Keys
|