Everything About Data Retrieval In PHP

Published on Jul 18,2019 2.2K Views

Everything About Data Retrieval In PHP

edureka.co

We need a database which is a collection of the tables to store the data in it which is entered by the user. This article will brief you on can creation of database and Data Retrieval In PHP.

Let us get started with the first of this article on Data retrieval in PHP,

How To Create A Database 

Step1: Open localhost dashboard and click on phpMyAdmin

Step2: Now create a database either through a query or manually. In order to do it manually, click on databases and create a new one.

Step3: Now create a table with columns as per our requirements for the website. As we are going to discuss how to design a feedback form using PHP, we just need to enter the name and feedback so we will just create a table with 2 columns.

Step4: Click on SQL and write a query for creating a table with the columns

 Moving on to the second bit of this article

Data Retrieval In PHP: PHP Code For The Feedback Form

Write this code in a notepad file and save it in htdocs and go to the localhost dashboard and run this code.


<?php
session_start();
// connect to mysql
$con= mysqli_connect("localhost", "root", "","feedback");
//In case of failure connection
if(!$con)
{
die("Server could not connected");
}
//After clicking on submit, those values get inserted into the table named as data which is located in feedback database.
if(isset($_POST["btn"]))
{
$name=$_POST["name"];
$feed=$_POST["feed"];
//mysql insert query
$sql="insert into data values('".$name."','".$feed."')";
$n=mysqli_query($con,$sql);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Feedback | Ashok Kumar |</title>
<meta name="distribution" content="global" />
<meta name="language" content="En" />
</head>
<script>
// function for creating alert after submission of feedback
function check()
{
alert('Thank you for your feedback.');
}
</script>
<div class="container">
<form id="contact" action="" method="post">
<h3>Feedback Form</h3>
<h4>"The key to learning is feedback.It is nearly impossible to learn anything with out it"</h4>
<fieldset>
<input placeholder="Your name" type="text" name="name" tabindex="1" required autofocus>
</fieldset>
<fieldset>
<textarea placeholder="Write Something about me.." name="feed" tabindex="5" required autofocus></textarea>
</fieldset>
<fieldset>
<input type="submit" name="btn" value="Submit" onclick="check()">
</fieldset>
</form>
</div>

After submitting the feedback, you can see the changes in the table under the database.

This bring us to the final bit of this article,

PHP Code For Data Retrieval From The Database

You can code it out in this way to fetch the feedbacks which is submitted.


<?php
// connect to mysql
$conn= mysqli_connect("localhost", "root", "","feedback");
if(! $conn )
{
die('Could not connect: ' );
}
// mysql select query
$sql = 'SELECT name,feed FROM data';
$n=mysqli_query($conn,$sql);
if(!$n )
{
die('Could not get data: ');
}
while($row = mysqli_fetch_array($n, MYSQLI_ASSOC))
{
echo "{$row['name']} ". " : {$row['feed']} <br> ";
}
//closing connection with server
mysqli_close($conn);
?>

And there you go

With this we come to an end of this article on Data Retrieval In PHP, I hope you have learned about  how to create a Database and store the data obtained from the form which is entered by a user and fetching the user data from the database.

If you found this Data Retrieval In PHP article relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of  this article and I will get back to you.

BROWSE COURSES
REGISTER FOR FREE WEBINAR Building Robust Applications with Spring Framework