How to retrieve data from a SQL table in PHP

Hi guys, today I am going to show you how can we retrieve data from a SQL table and show it on a PHP page.

In my previous post I have showed you how to insert data to a SQL table from a PHP page. So for this blog I am using that web interfaces and database and other related tables. Because there will be some more related blog posts like update and delete data, based on this same structure.

web form

Figure 1

Today I am adding an additional button named as Show to our web interface that we have used in the last post and it will look like Figure 1.

You have an idea about the Cancel and Submit button if you have read the previous post. Now I will explain what will happen when you click on the Show button. It will direct you to a PHP page which shows all the entries relevant to the table. But according to my interface you can’t put the relevant PHP page in the form action attribute. As you can see there is a Submit button also. Therefor you have to insert the relevant PHP page in the onClick attribute of the buttons like follow.

input type="submit" name="buttonShow" id="buttonShow" value="Show" onClick="form.action='showdb.php';" 

Following is the  PHP code for retrieve data from the SQL table .

$retreiveQuery = "SELECT * FROM member_details";
$retreiveResult = mysql_query($retreiveQuery) or die ("Query failed: ".mysql_error());

while($row1 = mysql_fetch_array($retreiveResult)){
	echo ": ".$row1['nic']."br tag";
	echo ":::".$row1['name']."br tag";
	echo ":::".$row1['age']."br tag";			
	echo "br tag";

// redirect code goes here

The br tag means “<br />” I can’t use it there because it won’t show as a tag in this blog template. The output is like this.

Show Output

Figure 2

I wanted to show you how can we take just one entry and place them in different fields. If I show in this post it will be a very lengthy post. Therefor I will describe it on another post.

See ya🙂

About AnujAroshA

An undergraduate in the stream of ICT (Information & Communication Technology). A simple person :)
This entry was posted in PHP examples. Bookmark the permalink.

2 Responses to How to retrieve data from a SQL table in PHP

  1. Pingback: Retrieve data from a MySQL table and store them as global variables | Anu's crazy world

  2. Pingback: How to update MySQL table in PHP | Anu's crazy world

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s