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

Advertisement

Validates Username and Passowrd with PHP

Hi Guys, Here I m Writing the code, How to Validates user name and password with the database…

If the username and Password matches with the database values we need to assign a session to that user..

for that here we are giving a simple session to a user.

<?PHP

session_start();

$server = “localhost”; //In case if u have some other write it here
$username = “Your Server Login Username”;
$password = “Your Server Login Password”;
$db_name = “Your Database Name”;

$db = mysql_connect($server, $username, $password) or die(“Connection to database failed, perhaps the service is down !!”);
mysql_select_db($db_name) or die(“Database name not available !!”);

Those Query Will Connect You with your Database…. Now I m Writing the Query..

$login = mysql_query(“select * from table_name where (username = ‘” . $_POST[‘username’] . “‘) and (password = ‘” . md5($_POST[‘password’]) . “‘)”,$db);

Tip: here my table contains only Two Field user name and password, if ur table contains more that two fields you can also use *. There is no such issue of using (*) Specifically. (*) will select all the values from the database, where as I can also write

$login = mysql_query(“SELECT username, password FROM table_name WHERE (username = ‘” . $_POST[‘username’] . “‘) and (password = ‘” . md5($_POST[‘password’]) . “‘)”,$db);

Note: Here first username you are watching is the name of the column in your database and the value $_POST[‘username’] is the value which we are getting from the form in the previous page. Similarly for password also.

Note2: md5($_POST[‘password’]) is the Password which is stored in the database in the form encrypted value, so that no one can see what the password is!!

$rowcount = mysql_num_rows($login);
if ($rowcount == 1) {
$_SESSION[‘username’] = $_POST[‘username’];
header(“Location: welcomehome.php”);
}
else
{
header(“Location: login.php”);
}
?>

Note: here we are giving username and password to a variable called $rowcount.

Next if the user name and password matches with the database we are assigning user a session. and If user name and password doesn’t matches with the database we are redirecting the user back to the login page, else we will redirect the user to its home page.

Hello world!

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!