In previous tutorial, we have seen how to “INSERT DATA IN MySQL USING PHP” . In today’s tutorial we will see how to display data from MySQL using PHP.

The method to Fetch MySQL table data using PHP

In this code we have used :

  • mysql_connect() : Used to open connection in MySQL and takes three parameter i.e. SERVER,USER_NAME and PASSWORD. we take default values for these parameters i.e. localhost, root and empty string respectively. This function returns a MySQL link identifier on successful connection and return false if fails to open connection.
  • mysql_error() : this returns the text of the error message  from previous MySQL operation and empty string if no error occurred.
  • mysql_select_db() : Sets the current active database on the server which is associated with specified link identifier returned by mysql_connect().
  • mysql_query() : This sends a query to current active database on the server. It takes SQL query as parameter.
  • mysql_fetch_row() : This function get a result row as an enumerated array. It returns numerical array corresponds to the fetched row and moves the internal data pointer ahead.   mysql_fetch_row() fetches one row of data from the result associated with the specified result identifier. The row is returned as an array and each result column is stored in array offset which starts at offset 0.
<html>
<body>
   <center>
       <h2 align="center">Employee Data</h2>
           <table border="1">
              <tr align="center"> 
                   <th>Emp_Id </th><th>Emp_name </th> <th>Age</th> <th>Designation</th> <th>Salary</th> 
              </tr>
 <?php
          $con=mysql_connect("localhost","root","");
          if(!$con)
          {    echo "Connection error".mysql_error();
          }
          else
          {
               $res=mysql_select_db("employee",$con);
               if(!$res)
               {     echo "Database not Found...".mysql_error();
               }
               else
               {
                   $res=mysql_query("Select * from emp");  // Query to select data from emp table
                   echo "<tr><td colspan='5'></td></tr>";  // This row has been added just for formatting
                   while($r=mysql_fetch_row($res))        
                   {
                           echo "<tr>";
                            echo "<td align='center'>$r[0]</td>";  // $r[0] is empId...
                           //$r[1] is firstName & $r[2] is lastName , both have been displayed together  
                           echo "<td width='200'>$r[1]" . " $r[2]</td>";                           
                           echo "<td align='center' width='40'> $r[3]</td>";
                           echo "<td align='center' width='120'>$r[4]</td>";
                           echo "<td width='100' align='center'>$r[5]</td>";
                           echo "</tr>";
                     }
                }

          }
 ?>
            </table>
      </center>
    </body>
 </html>

Output :

emp_Display