Connect, Insert, Update, Delete Data from database using PHP - Part -1

In this series of posts we are going to show you how to connect your mysql database with your basic PHP Application.

We are going to use basic mysqli_connect connectivity function. But there are various ways to connect to mysql Database like:
  • Simple mysql_connect which is now deprecated.
  • Advanced version of mysql_connect i.e mysqli_connect.
  • PDO - PHP Data Objects 
In these series of  Posts lets see what are the basic stuffs you will be needing and some of the things which will be helpful to code pages easily.
  • Xampp (Cross platform) Server/ Wamp (Windows Based) Server/ Lamp (Linux Based) Server
  • Here are the download links for the same
  • You need to create a folder in 'www' directory of WAMP/LAMP OR in  'htdocs' folder of XAMPP to create a new project.
    • For WAMP this folder's default location will be C://WAMP/www
    • For Linux XAMPP you will find this folder in opt/lampp/htdocs
    • For Windows if xampp is installed in the default directory then it will be in C://Xampp/htdocs
  • Now create a folder, let say i will be working in 'MyFirstProject' folder in this whole tutorial.
  • The second thing you will need is a Text Editor for coding purposes. Here are some links to few of them:
  • Download and install any one of the Text editor which suits you.

Now Lets Code a simple Hello World in PHP and HTML so that you get familiar with php.

<?php
echo "<html>
      <head><title>Hello World</title></head>
      <body>
      <h1>Hello World</h1>
      </body>
      </html>"
?>


  • The above code has a basic php tag which is <?php ?>
  • And a function echo which echos/prints whatever is passed inside it.

The Connection Code:

Now let us start with the Connection part where we will establish connection of our php application with the mysql database.

Follow below points step by step :
  • Start your WAMP server. The system tray icon should turn green.
  • Open http://localhost/phpmyadmin in your browser to open PHP-MY-ADMIN of your wamp server.
    • This is the link to open the UI version of your sql database.
    • If your WAMP server is not running, than this link won't open.
    • The screen will be divided into two frames one is the left side (say Left Panel) - having list of the databases and one is the right side (say Right Panel) - to do all the operation on the selected database/ its table.
  • Now Create a New Database. Lets name it test_db.
    • To create a new Database click on the Database menu in the right panel.
    • In the databases window(right side) type the name of the database in the database name text box.
    • Keep the collation as it is and click on the Create button.
    • As now I want to show you all the CRUD operations, lets make a table which has basic info of a person like first name, last name, gender, current city and age.
    • Watch the video below to get help!
    • As the database and a table inside is created as shown in the video. Now we will need to write the code for database connectivity.
  • First of all, we have to options to choose:
    • Write the connection code in each and every php file which need the database connection. (i.e Files doing CRUD operation)
    • Or else Make a separate file which only has connection code and include that file inside other files which need the database connectivity. using the default inbuilt include function of php.(recommended)
  • Here I will write the code and further in the project I will use the include function to include  that file. If you opt to choose the first option than don't make a separate file here and just write this code of connection in those files which will be explained in further posts. For inserting the data, displaying the table data, searching the data, and so on.
  • File Name: Connection.php
  •  
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    
    <?php 
    $conn=mysqli_connect("localhost","root","","test_db") or die(mysql_error());
    /*The above code is having a function mysqli which takes four parameters.
    First parameter tells the host name
    Second parameter tells the username of database (default is root)
    Third Parameter is password of database(which is by default nothing hence
    only pair of double quotes are passed.
    fourth parameter is the database name.
    After that the "or die()" function is used to stop the page from being scanned 
    after that line of code if in case the database connection fails 
    and it will display the mysql_error() instead.
    */
    ?>

  • In the above code the lines written inside /* */ are comment and you can remove them.
  • Just refer it once and then u can remove the whole block of code inside  /* */ including it.
  • Save the code with the above file name inside your project folder MyFirstProject as I told earlier in this post.
  • Now make another File named index.php (Every project has default file index.php as the first file).
  • Write the below code in that file.
  • 1
    2
    3
    4
    5
    6
    7
    8
    <?php
    include("Connection.php");
    if($conn==true) //This line checks weather the connection variable's 
                    //is connected to database or not.
     echo "Connected!";
    else 
     echo "Not Connected!";
    ?>
    
  • The above code is not at all related to this project. It is just checking weather the connection is set or not. The variable "$conn " is used in the file Connection to connect to the DB and it is used here to check wheather the connection is established or NOT.
  • Still as we have used the "or die()" function in the Connection.php file the else portion makes no sense but its just for beginners to understand some basic things and get in to the coding environment. Nothing else! :-p

This is the end for this post! As we have successfully connected DB to our application. In the upcoming Post we will create an HTML form to insert the details of that form in the database and we will tweak our index.php file to show the links for various other pages including the insert details page, update details page, search Page, DB dump page, Delete Page.

Till then get familiar with HTML and PHP. Also try to learn a bit of CSS. But for this one there is no need of learning CSS. As This is just for beginners to learn the CRUD operations with PHP.


Update (11/8/2016):

The files in this tutorial can be found in Tutorial 1 directory of this series of post in our Github repository

Have a Happy Coding.

Comments

  1. I have never read more interesting articles than yours before. You make me so easy to understand and I will continue to share this site. Thank you very much and more power!
    PHP Institutes in Chennai
    PHP Training Center in Chennai

    ReplyDelete
  2. Such an interesting article on the recent talks in the software industry, hope this article helps everyone to update yourself.
    web development courses in chennai with placement
    Web designing training centers in chennai

    ReplyDelete

Post a Comment

Popular posts from this blog

Windows phone Wifi connectivity problem.

Create Excel file using Java: Apache POI - PART - 2: Line Chart