SQL & PHP Tutorial

create mysql table using php


In this tutorial you'll learn how to Create SQL table using PHP.

Create database - Video tutorial


SQL create database

How to create mysql table using php

    /_?php
	$dbhost = 'localhost:3306';
           //SERVER:
           //Optional − The host name running the database server. If not specified, then the default value will be localhost:3306.
    $dbuser = ‘client';
          //User: 
          //Optional − The username accessing the database. If not specified, then the default will be the name of the user that owns the server process.
    $dbpass = ‘client123';
          // Passwd: 
          //Optional − The password of the user accessing the database. If not specified, then the default will be an empty password.
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
          //new_link:
	      //Optional − If a second call is made to mysql_connect() with the same arguments, no new connection will be established; instead, the identifier of the already opened connection will be returned.
    if(! $conn ) {
     die('Could not connect: ' . mysql_error());
	 }
	      // If connection could not established then show “Could not connect”
	echo 'Connected successfully';
          //If connection established then show “Connected Successfully”
   $sql = 'CREATE DATABASE Shaigle_test';
   //sql:
   //Required - SQL query to create or delete a MySQL database
   $retval = mysql_query( $sql, $conn );
   if(! $retval ) {
      die('Could not create database: ' . mysql_error());
   }
   //If database could not created then show “Could not create database”
   echo "Database Shaigle_test created successfully\n";
   mysql_close($conn);
   //You can disconnect from the MySQL database anytime using another PHP function mysql_close().
   ?-/

MySQL Database created, now its time create Table

/* Once the database has been created, you can create a table to store data that you retrieve from forms.
To create a table, you must provide a name for the table, and the names and data types of each column in the table. It is also a good practice to indicate whether null values are allowed in each column.
The table created in this step will have three columns:

ProductID, ProductName, Price and ProductDescription.
The table will be called “Product”
*/

  /_?php
  
   $cn = mysql_connect('localhost', ‘client', ‘Client123') or
        die('Unable to connect to server');
       //This creates a connection to the MySQL server on Localhost with the username “client” and the password “client123”.
		//If you are using phpMyAdmin then click on
		
	
phpMyAdmin
   mysql_select_db("shaigle_test", $cn) or	
      die(mysql_error($cn));
    	//Connect to the database, if not then show error
	
database-select

//Select database 'shaigle_test'

database-select-name
   CREATE TABLE dbo.Products 
     //Where dbo ‘aoptional’ stands for database owner and Product is name of sql table
    
sql-new-tabel

//select Tables then click New Button

table_name

//The columns in the table are named
//ProductID
//ProductName
//Price


//ProductDescription

column-name

   (ProductID int PRIMARY KEY NOT NULL, 
    ProductName varchar(25) NOT NULL,
	Price varchar NULL,
	ProductDescription text NULL)

    mysql_close($conn);
   //disconnect from the MySQL database
  

//Table Created with four column

fill-column
 ?-/

All together

    /_?php
     $cn = mysql_connect('localhost', ‘client', ‘Client123') or
        die('Unable to connect to server');
        
     mysql_select_db("shaigle_test", $cn) or	
      die(mysql_error($cn));
      
      CREATE TABLE dbo.Products 
      
    (ProductID int PRIMARY KEY NOT NULL, 
    ProductName varchar(25) NOT NULL,
	Price varchar NULL,
	ProductDescription text NULL)

    mysql_close($conn);
      
    ?-/
    
SQL insert data into table