www.pickatutorial.com Computer Tutorials

PHP creating MySQL tables. Free online PHP and MySQL Web Database Programming Tutorial...

Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script
Lessons Creating MySQL Tables Bookmark and Share
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
Lesson 41
Lesson 42
Lesson 43
Lesson 44
We use the CREATE TABLE statement to create a table in MySQL. Its syntax is as under:

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.


Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.