How to Get Data from Database using PHP and mySql

How to Get Data from Database using PHP and mySql

Today I will explain how to get data or records in database using PHP  and mySql. This topic is very important part of any web developer at beginner to advance level.

I know most of people recommended mysql database because it is free and easy to understand. This post is simple get data or records from database later I will explain how to edit/update data or records in database also how to delete data or records from database using php and mysql.

In this post, following file are required.


Follow These Steps.

Step 1.   See First How to Insert data in Database using php and mysql

Step 2.   Create View File
      Go to where xampp is installed and open htdoc folder then go to tutorials  folder and create a new file for example, view_stu.php , Code shown below

      <table align="center" class="table " border="1">
       <thead>
        <tr style="background-color: gray; color: white;">
          <th>S.No</th>
          <th>Name</th>
          <th>Gender</th>
          <th>Email</th>
          <th>Phone No</th>
          <th>Address</th>
        </tr>
      </thead>
      <tbody>
        <?php
           include_once('config.php');

           $data="select * from students";
           $data = $conn->query($data);
     
           while ($row = $data->fetch_array())
          {

            $stu_id            = $row['stu_id'];
            $stu_name       = $row['stu_name'];
            $stu_email       = $row['stu_email'];
            $stu_gender     = $row['stu_gender'];
            $stu_phone      = $row['stu_phone'];
            $stu_address    = $row['stu_address'];

            echo("
                     <tr>
                      <td>$stu_id</td>
                      <td>$stu_name</td>
                      <td>$stu_gender</td>
                      <td>$stu_email</td>
                      <td>$stu_phone</td>
                      <td>$stu_address</td>
                    </tr>
                 ");
           }

      ?>
    </tbody>
  </table>

Step 3.   Open insertstu.php File
      I will some changes in insertstu.php, if you already created otherwsie yon can create new file insertstu.php, code shown below

          <?php

              include_once('config.php');

              $name       = $_POST['name'];
              $gender     = $_POST['gender'];
              $email       = $_POST['email'];
              $phone      = $_POST['phone'];
              $address    = $_POST['address'];

              $data = "insert into students (stu_name, stu_gender, stu_email, stu_phone, stu_address) values ('$name', '$gender', '$email', '$phone', '$address')";
              $result = mysqli_query($conn, $data);

              if(isset($result))
                {
                   echo "<script>alert('Do you want to  insert new student record');window.location='view_stu.php?p=1';</script>";
                }
             else
                {
                echo "Student Entry not Found";
                }

          ?>

Step 4.   Now, go to browser type localhost/tutorials/addstu.php, screen shown below.


Get Data from Database using PHP and mySql

Now insert data in required filed then press submit button, screen shown below.


Get Data from database using PHP and mySql





                   I hope, it helped you to understand, how to get data from database using PHP and mySql......Thanks

About MSK TUTORIAL

Hi...I am Md Sahjad Karim and I am Engineering Graduate (B.E in Computer Science and Engineering) working as a Web Developer and write Blog Articles. http://www.msktutorial.com/ was launched on july 2016 to provide free online hints, tips, guides and tutorials related to technology(PHP, CodeIgniter, WordPress, Blogger etc). My primary goal is to provide the good, quality and easily to understand the articles to readers around the world.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment

Thanks For Visiting Blog...