Tuesday, September 4, 2012

Create Database Tables using Php and MySql


     


 To create database and tables using Php and MySql we have to do two simple steps ie.

  1. Create a php file which has the MySql code to create database ,tables and upload the php file to the server.
  2. Execute the php file from the browser .

Create a php file as Create_Database.php

<?php
//connect to database 

$con = mysql_connect("localhost","root","");

if (!$con)
  { die('Could not connect: ' . mysql_error()); }


// drop database 

$drop = "drop database todoo";
mysql_query($drop);

//create db todoo

$sql="CREATE DATABASE todoo";
if (mysql_query($sql,$con))
{ echo "Successfully Created Database.."; }
else
{ echo mysql_errno().':'.mysql_error(); }
 
 mysql_select_db('todoo');
 
//create table 

$sql="CREATE TABLE tasks 
  (task_id int(4)          AUTO_INCREMENT,
   title varchar(30)   NOT NULL,
   description varchar(100)  NOT NULL ,
     PRIMARY KEY (title),
     UNIQUE(task_id))";
   
if(mysql_query($sql))
{echo 'Successfully Created Tables..
';}
else
{echo mysql_error();}   

mysql_close();


Open Browser and select Create_Database.php ,which creates database and tables with following messages.


     

No comments: