Organizational Research By

Surprising Reserch Topic

Question:PHP:how to Creates Data-base and Tables using php and mysql?


A database holds one or more tables.


Create a Database

The CREATE DATABASE statement is used to create a database table in MySQL.

We must add the CREATE DATABASE statement to the mysqli_query() function to execute the command.

The following example creates a database named "my_db":

$con=mysqli_connect("example.com","peter","abc123");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// Create database
$sql="CREATE DATABASE my_db";
if (mysqli_query($con,$sql))
  {
  echo "Database my_db created successfully";
  }
else
  {
  echo "Error creating database: " . mysqli_error();
  }
?>



Create a Table

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

We must add the CREATE TABLE statement to the mysqli_query() function to execute the command.

The following example creates a table named "Persons", with three columns. The column names will be "Firstname", "Lastname" and "Age":

$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// Create table
$sql="CREATE TABLE persons(Firstname CHAR(30),Lastname CHAR(30),Age INT)";

// Execute query
if (mysqli_query($con,$sql))
  {
  echo "Table persons created successfully";
  }
else
  {
  echo "Error creating table: " . mysqli_error();
  }
?>

Note: When you create a database field of type CHAR, you must specify the maximum length of the field, e.g. CHAR(50).


Primary Keys and Auto Increment Fields

Each table in a database should have a primary key field.

A primary key is used to uniquely identify the rows in a table. Each primary key value must be unique within the table. Furthermore, the primary key field cannot be null because the database engine requires a value to locate the record.

The following example sets the PID field as the primary key field. The primary key field is often an ID number, and is often used with the AUTO_INCREMENT setting. AUTO_INCREMENT automatically increases the value of the field by 1 each time a new record is added. To ensure that the primary key field cannot be null, we must add the NOT NULL setting to the field:

$sql = "CREATE TABLE Persons 
(
PID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(PID),
Firstname CHAR(15),
Lastname CHAR(15),
Age INT
)";

 

asked Sep 13, 2013 in PHP by rajesh
edited Sep 12, 2013
0 votes
20 views



Related Hot Questions



Government Jobs Opening


...