DATABASE PHP

MYSQL Create Table || MYSQL Create Database

MYSQL Create Database
Mysql is a relational database.It stands for “My Structured Query Language”.If we want to use mysql database with php,than we have to install one of the web server solution stack package “XAMP” or “WAMP”.As php is server-side language.So we can manipulate mysql database with php to MySQL Create Table.

Database Connection:

A function mysql_connect is use to connect php and mysql.It has four parameter such as name of server, user, password and database.For just connection database is not compulsory , it can be created after connection .

Example #1:

<?php
$server = "localhost";
$name = " Your Username";
$password = "Your password";


$connection = mysqli_connect($server, $name, $password);
 
if($connection){  // False if connection don't occur 
    echo "Connected successfully";  
}else{
    echo "Not Connected";
}
?>

Output:

Connected Successfully

Create Database:

Database is created after the connection between php and mysql is established.Php server execute the sql query for creation of Database.

Syntax:

CREATE DATABASE YOUR_DATABASE_NAME

Example #2:

<?php
$server = "localhost";
$name = "Your Username";
$password = "Your Password";

$connection = mysqli_connect($server, $name, $password);

$query="CREATE DATABASE TEMP";

$result=mysqli_query($connection,$query);

if($result){   // True if connection don't occur 
    echo "Database is created";
}else{
    echo "Database is not created";
}

Output:

Create Table:

Table is collection of rows and column.In mysql  column is the label of each cell. Each column can have only one datatype.Declare width of all the cells of each column with it,for example: int(10).The generally use datatypes are following:

Datatypes:

  • INT
  • VARCHAR
  • TEXT
  • DATE
  • JSON

Primary key:

The Primary key is that column of MYSQL Create table which has unique value.Because of its values,each row is reachable and can easily modified.Its value can’t be null.

Foreign key:

The Foreign key is the reference from one table to another.Its generally use to make a relation between two tables.

Example #3:

<?php
$server = "localhost";
$name = "Your Username";
$password = "Your Password";
$database="TEMP";

$connection = mysqli_connect($server, $name, $password,$database);

$query = "CREATE TABLE STUDENT (
    id INT(10) AUTO_INCREMENT PRIMARY KEY, 
    name VARCHAR(40) NOT NULL,
    father_name VARCHAR(40) NOT NULL,
    marks int(10) NOT NULL,
    phone int(13) NULL,
    address VARCHAR(100) NOT NULL
    )";


$result=mysqli_query($connection,$query);
 
if($result){
    echo "The Table is created";
}else{
    echo "The Table is not created";
}
?>

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *