Adscend

Click Here



Click Here



Click Here

Tuesday 3 September 2013

Create a Table


Create a Table


1.The CREATE TABLE statement is used to create a table in MySQL.
2.The CREATE TABLE statement has to add to the mysql_query() function to execute the command.
3.A database must be selected before a table can be created.
The database is selected with the mysql_select_db() function.

Syntax
CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name3 data_type,
....

)

Tables consists rows and columns and each table must have a name.

Example
<?php
$hostname = "localhost";
$username = "root";
$password = "";
$con = mysql_connect("$hostname", "$username", "$password");
//$con = mysql_connect("localhost","root","");
if (!$con)
{
die("Could not connect: " . mysql_error());
}
// Create database
if (mysql_query("CREATE DATABASE Aspell",$con))
{
echo "Database created<br>";
}
else
{
echo "Error creating database: " . mysql_error();
}
// Create table
mysql_select_db("Aspell", $con);
$sql = "CREATE TABLE Persons
(
FirstName varchar(15) NOT NULL,
LastName varchar(15) NOT NULL,
Age int NOT NULL
)";
// Execute query
$retval = mysql_query($sql,$con);
if(! $retval )
{
die("Could not create table: " . mysql_error());
}
echo "Table created successfully";
mysql_close($con);
?>
Output
Database created
Table created successfully



No comments:

Post a Comment