Creating a table in the database with php

After creating a database for our use, the next step is to create a table where we are going to enter the values.

As in the MySQL,the CREATE TABLE command is used to create a table in the database. Here in the following program, we are going to create a table named firsttable. Before creating a table we need to declare the database where we are going to create the table.



<?php
$servername="localhost";
$username="root";
$password="";
$dbname="mydb";
//connecting...
$conn=new mysqli($servername,$username,$password,$dbname);
//checking...
if($conn->connect_error)
{
    die("connection failed:".$conn->connect_error);
}
//creating table
$sql="CREATE TABLE firsttable (num INT(6) primary KEY key,user VARCHAR(20))";
if($conn->query($sql)===TRUE)
{
    echo "table sucessfully created";
}
else
{
    echo "error in creating the table:".$conn->error;
}

$conn->close();
?>




Output:

table created sucessfully



The $sql is used to insert sql query in the php. The conditional operator if checks weather the table is created or not. It checks it by checking the using the identical operator weather the statement returns TRUE or FALSE value. If the table returns a TRUE value it means that the table is created. If it returns FALSE, it means that the table is not created.

The $conn->query($sql) returns the current status of the last sql command that has been executed. The $conn->connect_error returns the error message related to the connection with the database.

Here we created a table with 2 columns as num that can store integer valuse and user that can store variable characters.

Prev
Next

Comments

Popular posts from this blog

Node.js Cheat Sheet

Codeigniter ! Simple But Powerful

Bootstrap ? What is it ?