Monday, 10 June 2013

How to create a MYSQL database in PHP

Creating a MYSQL database in php is a simple task. 

There are 2 steps in the process
  • Connect to MYSQL database
  • Fire a query to create a database in MYSQL
Example with Code and Synatx:



<?php
$mysqlcon=mysqli_connect("localhost","uid","password");

// connect to mysql using userid and password

if (mysqli_connect_errno())
  {
  echo "Error while connecting to MySQL: " . mysqli_connect_error();
  }

//*********************Connection Successful**************************
// Now you can create database

$querysql="CREATE DATABASE EMP";
if (mysqli_query($mysqlcon,$querysql))
  {
  echo "Database EMP created .......";
  }
else
  {
  echo "Error creating database: " . mysqli_error($mysqlcon);
  }
?>


In above example we have created the database called EMP. If there is any error while connecting  to MYSQL server or creating a database in MYSQL we are printing that as well.

No comments:

Post a Comment