SQL & PHP Tutorial

Create database in php


In this tutorial you'll learn how to Create Database in MySQL using PHP.

create database Video tutorial



The following is a basic PHP content to show Create database in sql server utilizing the php MS SQL driver.

    $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 TUTORIALS';
    

//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 TUTORIALS created successfully\n";
   
   mysql_close($conn);
    

//You can disconnect from the MySQL database anytime using another PHP function mysql_close().

All Together
    $dbhost = 'localhost:3306';
           
    $dbuser = 'client';
          
    $dbpass = 'client123';
         
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
          
    if(! $conn ) {
     die('Could not connect: ' . mysql_error());
  }
      
 echo 'Connected successfully';
         
          
   $sql = 'CREATE DATABASE TUTORIALS';
   
   $retval = mysql_query( $sql, $conn );
   if(! $retval ) {
      die('Could not create database: ' . mysql_error());
   }
  
   echo "Database TUTORIALS created successfully\n";
   
   mysql_close($conn);
  
SQL Create Table