Displaying Something from The Database

Guys in this tutorial I will explain you how to fetch values from the database and show those in a Table format.

Here I assumed one thing, that the user is logged in. and that user has the rights to delete a particular record from the table.

NOTE: This tutorial is very old. But will glimpse you how you can format your data in a structured manner.

So are you Guys Ready???? Then Let’s Start……..

<?PHP
session_start();
if (!isset($_SESSION['username'])) {
header("Location: login.php");
}
?>

view raw
gistfile1.phtml
hosted with ❤ by GitHub

Note: This will start the session of that particular user.

<?php$con = mysql_connect("localhost", "USERNAME", "PASSWORD");
if (!$con) {
die('Could Not Connect: ' . mysql_error());
}
mysql_select_db("DB_NAME", $con);
$result = mysql_query("SELECT firstname, lastname, email, phone, mobile, address_1, address_2, city, zipcode, country, state FROM TABLE_NAME");
echo "
<table border='1'>
<tr>
<th>S. NO. </th>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Phone No.</th>
<th>Mobile No.</th>
<th>Address</th>
<th>City & Zip</th>
<th>State & Country</th>
<th> Delete Entry</th>
</tr>";
$i = 1;
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $i . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['mobile'] . "</td>";
echo "<td>" . $row['address_1'] . ", " . $row['address_2'] . "</td>";
echo "<td>" . $row['city'] . ", " . $row['zipcode'] . "</td>";
echo "<td>" . $row['state'] . ", " . $row['country'] . "</td>";
echo "<input type='hidden' name='mobile' value='$row[mobile]' />";
echo "<td>" . "<input type='submit' name='submit' value='Delete'/>" . "</td>";
echo "</tr>";
$i++;
}
echo "</table>";
mysql_close($con);
?>

view raw
file1.php
hosted with ❤ by GitHub

3 thoughts on “Displaying Something from The Database

  1. Hi, i want to display the data in table format like this

    Username : Ranjith
    Mobilenumber : 9036375753
    email Id : ranjith123@yahoo.com

    the above mentioned data i need to display in the table…

    Please give solution for this…I am new to php

    1. Ranjith,

      You can use the below code snippet:

      Username
      Ranjith

      However, at this point of time I am unable to give you the proper snippet. But this will give you enough momentum to proceed.

      – Puneet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s