Data Retrieval In Php

Brief About Data Retrieval in PHP

Brief About Data Retrieval in PHP

To store the data entered by the user, we require a database, which is a collection of tables. This article will go over database creation and Data Retrieval in PHP.

How to create a database

Step 1 : Go to the localhost dashboard and click on phpMyAdmin

Localhost-Dashboard

Step 2 : Create a database with a query or manually. If you go with the manual, click on databases to create a new database.

Create A Database

Step 3 : Create a table with columns based on our website requirements. We only need to enter the name and feedback because we are going to discuss how to design a feedback form using PHP, so we will just create a table with two columns.

Step 4 : Click on SQL to write a query and create a table with columns

Create A Table With Columns

Now, we have successfully created a database and a table in SQL

Data Retrieval in PHP

Following is the code that performs data retrieval. Copy this code in a notepad and go to the localhost dashboard and execute the 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” “<a href=”http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>”>

<html xmlns=”<a href=”http://www.w3.org/1999/xhtml”>http://www.w3.org/1999/xhtml</a>”>

<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>”Learning will never end without feedback “</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>

Feedback Form

Feedback-Form

Once the feedback submission is completed, we can see the changes in the table of the database.

Feedback-Form

Data Retrieval from the database using PHP

The following code will be useful for you to bring the feedback that 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);

?>

Output

Conclusion

This is how data retrieval can be performed using PHP and SQL. Learn more about web development in our PHP Training in Chennai at Softlogic Systems.

Leave a Comment