Data Retrieval from Database in PHP – With Coding

QUICK ENQUIRY FORM



WHAT WILL HAPPEN WHEN YOU SUBMIT THIS FORM

  • After submitting the enquiry, you will be contacted by our team.
  • Payilagam Team will get to know about your training needs.
  • They will update you how could they help you.
  • Trainers(IT Professionals) may contact you if you need any clarification.
  • Your contact information will be kept highly confidential & not be shared to 3rd Parties.
Quick Enquiry

Data Retrieval from Database in PHP – With Coding

Data Retrieval from Database in PHP – With Coding

This blog explains about Data Retrieval from Database in PHP – With Coding and is explained below :

_______________________________________________________________________________

Hi,

Today we are going to know about how to retrieve data from database.  Here we are using mysql_fetch_array(); to get a particular row values. It stored all values in an array. Then we will call all values by its index names.

Retrieve or Fetch Data From Database

As we know Database is a collection of tables that stores data in it.

To retrieve or fetch data from MySQL database it is simple to do it using MySQL ” Select” query in PHP .

Using PHP script insert user entered data/information into database and extract/fetch data stored in database and display it on the browser.

Releasing Memory

Its a good practice to release cursor memory at the end of each SELECT statement. This can be done by using PHP function mysql_free_result(). Below is the example to show how it has to be used.

Example

Try out following example

<?php
   $dbhost = 'localhost:3036';
   $dbuser = 'root';
   $dbpass = 'rootpassword';
   
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);
   
   if(! $conn ) {
      die('Could not connect: ' . mysql_error());
   }
   
   $sql = 'SELECT emp_id, emp_name, emp_salary FROM employee';
   mysql_select_db('test_db');
   $retval = mysql_query( $sql, $conn );
   
   if(! $retval ) {
      die('Could not get data: ' . mysql_error());
   }
   
   while($row = mysql_fetch_array($retval, MYSQL_NUM)) {
      echo "EMP ID :{$row[0]}  <br> ".
         "EMP NAME : {$row[1]} <br> ".
         "EMP SALARY : {$row[2]} <br> ".
         "--------------------------------<br>";
   }
   
   mysql_free_result($retval);
   echo "Fetched data successfully\n";
   
   mysql_close($conn);
?>
__________________________________________________________________________________________________

Ok. This is our program,

<?php
$dbhost=”localhost”;
     $dbuser=”root”;
     $dbpass=””;
     $conn=mysql_connect($dbhost,$dbuser,$dbpass);
     mysql_select_db(“trainee”);
     $result = mysql_query(“select * from details
     where name=’Guhan'”);
     $row= mysql_fetch_array($result); // It stores values in an array
     for($i=0; $i<3; $i++)
     {
        echo $row[$i].”<br>”;
     }
?>

OUTPUT:

Guhan
Ganesan
979181****

 

By | September 12th, 2017 | Blogs | 0 Comments

About the Author: