Create a MySQL database using PHP

We will talk about MySQL database and how to create an database using PHP programming language.

Let’s start from pre requested tools.

We need a server, I like to use the XAMPP server, you can find it here
We also need a code editor, my suggestion is Visual Studio Code, but you can use Atom or Sublime text editor and other editors that you like.

First, install both of the tools that we need for this coding, you can follow the tutorial on how to install XAMPP on your machine Windows 10 from the video below:

After installing the XAMPP and code editor, it is time to create one folder and a PHP file, you can give it a name how you want, but I am using connect.php

Now let start coding ๐Ÿ™‚

Creating the database:

<?php
   $dbhost = 'localhost';
   $dbuser = 'root';
   $dbpass = 'rootpassword';
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);
   if(! $conn ) {
      die('Could not connect: ' . mysql_error());
   }
   
   echo 'Connected successfully';
   
   $sql = 'CREATE Database test_db';
   $retval = mysql_query( $sql, $conn );
   
   if(! $retval ) {
      die('Could not create database: ' . mysql_error());
   }
   
   echo "Database test_db created successfully\n";
   mysql_close($conn);
?>

Next is selecting the database:

<?php
   $dbhost = 'localhost:3036';
   $dbuser = 'guest';
   $dbpass = 'guest123';
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);
   
   if(! $conn ) { 
      die('Could not connect: ' . mysql_error());
   }
   
   echo 'Connected successfully';
   
   mysql_select_db( 'test_db' );
   mysql_close($conn);
   
?>

Now we will create a database tables

To create tables in the new database you need to do the same thing as creating the database. First, create the SQL query to create the tables then execute the query using mysql_query() function.

<?php
   
   $dbhost = 'localhost:3036';
   $dbuser = 'root';
   $dbpass = 'rootpassword';
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);
   
   if(! $conn ) {
      die('Could not connect: ' . mysql_error());
   }
   
   echo 'Connected successfully';
   
   $sql = 'CREATE TABLE employee( '.
      'emp_id INT NOT NULL AUTO_INCREMENT, '.
      'emp_name VARCHAR(20) NOT NULL, '.
      'emp_address  VARCHAR(20) NOT NULL, '.
      'emp_salary   INT NOT NULL, '.
      'join_date    timestamp(14) NOT NULL, '.
      'primary key ( emp_id ))';
   mysql_select_db('test_db');
   $retval = mysql_query( $sql, $conn );
   
   if(! $retval ) {
      die('Could not create table: ' . mysql_error());
   }
   
   echo "Table employee created successfully\n";
   
   mysql_close($conn);
?>

That’s all, a database table created!
You can copy this code and it is free, but for a better understanding of how it works, try to write it line by line and see what you did, you will learn much more.
Do you have some suggestions for this code, can we Improve it together?
Be free to leave a comment ๐Ÿ™‚ Thank you.

One Comment

  1. Why you are using deprecated methods? Please educate yourself before putting something on the internet…

Leave a Comment

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