How to Fetch Data From Database and Display in Table in PHP

By | April 27, 2013

Hello Friends today i am going to put a tutorial related to How to Fetch Data From Database and Display in Table in PHP. Task is very easy what you need is a beginner knowledge of Mysql and PHP. First of all we will discuss how we will send the data to the database. HTML Form We will use the html form for getting the data from user. We will include the 2 fields Firstname and Lastname.

<form action="<?php echo $_SERVER["PHP_SELF"]?>" method="post">
<table>
<tr>
<td>First Name</td><td>:</td><td><input type="text" id="first" name="first"></td>
</tr>
<tr>
<td>Last Name</td><td>:</td><td><input type="text" id="last" name="last"></td>
</tr>
<tr>
<td></td><td></td><td><input type="submit" id="submit" name="submit"></td>
</tr>
</table>
</form>

We will send the value to page itself so here we are echoing the form values to the same page. By using the <?php echo $_SERVER[“PHP_SELF”” ?>. Here we are using the form method as POST. After filling the values we will get them and put them in variable. Use the below code.

<?PHP
if(isset ($_POST["submit"]))
{
$hostname="localhost";
$datauser="root";
$datapass="";
$dataname="values";

$con= mysqli_connect($hostname,$datauser,$datapass,$dataname)
or die("Error in connection");

$firstname=mysqli_real_escape_string($con,$_POST["first"]);
$lastname=mysqli_real_escape_string($con,$_POST["last"]);
if($firstname==""||$lastname=="")
{
$result="<span style='color:red'>Error please check</span>";
}
else
{
$insert="Insert into myvalues(firstname,lastname) values('$firstname','$lastname')";
$result=mysqli_query($con,$insert);
if($result)
{
$result= "<span style='color:green'>value entered</span>";
}
else
{
$result="<span style='color:red'>Error try again</span>";
}
}
mysqli_close($con);
}
?>

Let me describe the above PHP Code. First of all we will check if submit button has been pressed or not. If it is pressed than we will move to 2nd Point We will make a connection with our database and see whether is it connected or not. After connection we will get our values in our variable and check whether it is empty or not. If value is empty than it will return an error else it will perform the insert command. If the value is inserted into the database correctly than it will show the success else error. Now we have the values in our database and we have to show them on our page as HTML table. So we will return our database values as table with the PHP code. Showing the values in Table


Here i am discussing the very easy way to get the values and show them as table. I am using the php code for it and at last i will return that function over my html file.

<?php

function getValues()
{
$hostname="localhost";
$datauser="root";
$datapass="";
$dataname="values";

$con= mysqli_connect($hostname,$datauser,$datapass,$dataname)
or die("Error in connection");

$list="select * from myvalues";
$result=mysqli_query($con,$list);
$count=mysqli_num_rows($result);
if($count < 1)
{
return "<span>No any value</span>";
}
else
{
$alert="<table>";
$alert.="<tr><td>Sno.</td><td>First Name</td><td>Second Name</td></tr>";
while($row= mysqli_fetch_array($result))
{
$alert.="<tr><td>$row[Id]</td><td>$row[firstname]</td><td>$row[lastname]</td></tr>";
}
$alert.="</table><div></div>";
return $alert;
}
}
?>

In the above code we will do the following process.

We will select our rows and count them. If $count return value less than 1 than it will show the error else it will show the table.

In the above code we have echoed the whole table in PHP. While loop will iterate until we will  not get the last record from the table. It will iterate and append it to $alert. After appending we will code the table tag and just return the $alert. Now we have our whole bunch of rows in our $alert.

Now we will just call the function over our page to get the table. <?php echo getValues();?> Just put this code and it will show the table over your page.

I hope you like the tutorial. Happy coding. Thank you leaver your valuable comments.

Leave a Reply

Your email address will not be published. Required fields are marked *