PHP Create Database and Tables Dynamically Using MySQLi

By | September 26, 2014

Sometimes we forgot to create database and run our query than we got an error that is ” No Database with this name”. So I thought to figure it out and solve this issue by creating the database at the runtime if it is not available. If we do this we will never get this “No Database” error.

For doing this we will use our mysqli functions which is easy and very known for those who uses them. So lets get started.

Step 1. Define our database details

We will setup some variable so we will not write the details every time. We are using XAMPP at our end and we will do it over localhost.

$dbname="mydemo"; // Your database name
$dbpass=""; // Database password
$dbuser="root; // Database username
$dbhost="localhost"; // Database host

After setting the database variables we will move to our next step.

Step 2. Try to connect with database

Now we will try to connect the database using mysqli_connect() with the above details.

$con=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname) or die("error while connecting");

If it will not connect than it will give us the error “error while connecting” otherwise it will connect. In next step we will check is is connected or not if not than what we can do.

Step 3. Check connection

For checking we will create an if else.

if($con){
echo "connected";
}
else{
  echo "not found";
}

If it is connected it will echo the “connected” else it will echo the “Not Found”. Now if it is not connected than we will write some code to create the database.

Step3. Create the Object of mysqli to Create Database Dynamically

Now we will create the object of mysqli to create the database if it not connected.

$mysqli=new mysqli($dbhost,$dbuser,$dbpass);
$sql="CREATE DATABASE $dbname";
$mysqli->query($sql);

By using this it will create the database with the name of “mydemo” or whatever you want to put.

Now we will connect this again so that we will use it. We will just call a line again which we previously called.

Step 4. Connect Again

$con=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname) or die("error while choose");

Now if you try if else than you will see that it is connected because database is available and you will not get any error.

Step 5. Whole code at once

$dbuser = 'root';
$dbpass = '';
$dbname = 'onboard';
$dbhost = 'localhost';
  
$con=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname) or die("error while connecting");
echo "checked";
if($resource){
echo "connected";
}
else{
  echo "not found";
  $mysqli=new mysqli($dbhost,$dbuser,$dbpass);
$sql="CREATE DATABASE onboard";
$mysqli->query($sql);
$con=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname) or die("error while choose");
echo "connected";
}

 Step 6. Now you can create tables.

You can create the tables now using the connection string.

$table ="CREATE TABLE demotable(
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `myName` varchar(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
)";
  $result=mysqli_query($con, $table) or die(mysqli_error($con));

  if($result){echo "table created";}else{echo "nop";}

Hope you like our tutorial for Create Database and Tables Using Mysqli connection string. If you have any issues than please comment below.

 

Leave a Reply

Your email address will not be published. Required fields are marked *