Retrieve data from a MySQL table and store them as global variables

Hi… ! As I have promised you in my previous post, today I am going to explain you how to retrieve just one entry from a MySQL table and store those values in a SESSION variable which gives you globally access ability.

This is going to be a very small post if I only explain about the above mentioned functionality. Therefor I thought to explain simple Text Filed attributes as the first part of this post. Those attributes are title and placeholder.

First take a look at about the placeholder attribute. Watch the Figure 1 carefully concentrate on the NIC field. The text field is already filled with some text in a gray color when the page loads. In this scenario the text phrase is “National Identity Card”.  But when you click in the Text Box, those words in gray color

form interface
Figure 1

will disappear. This attribute we use to help the users to guide what he/she should write in that relevant text field or the right format that should enter in that text field.

There is another way to guide the users without mess up our form. That system is, when the user moves his/her mouse pointer on top of the text field, a hint will appear  by the side. Now look at the Name field. Actually my mouse point is on top of the Name text filed although you cannot see that in this picture. There they gave the hint “Full Name” side of my mouse pointer. Here I have used the title attribute in the Text Field to activate this feature.

The relevant code for both of above mentioned attributes as follows.

// placeholder attribute
input type="text" name="textfieldNic" id="textfieldNic" placeholder="National Identity Card"


// title attribute
input type="text" name="textfieldName" id="textfieldName" title="Full Name"

Before move to our main target just take a look at my previous post because I’m going to continue from that point. Now you are capable of retrieving all the data from MySQL table. Assume you want to get the details of a particular ID of that given table. Then how you are going to achieve that task with your PHP codings.

Figure 2

Here is an example. When you provide a valid NIC number to the text box and press View button (Figure 2), it will direct to a PHP page which is going to perform as you wanted in above paragraph.

Here is the code of that PHP page.


$var_nic = $_POST[@textfieldNic];

//Data retreive from the db

	//echo "Please enter your NIC number";
	// Redirect to the page to enter NIC number

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

	$row1 = mysql_fetch_array($retreiveResult);




	// Redirect and display the result
        //header("Location: custom_view.php");

As you can see, what I have done here is, first checked the user has given a value to the text box in previous page and if not redirect him/her again to the page to give a NIC number. Then I took the value given by the user and check it from the table where my member details located and if there is a value which match the user input, then I get those values and pass to some variables which starts with $db_var_* Next I store those values as session variables because then I can call them in a different page (custom_view.php) as I have done in below.

Figure 3

You can get session variables by using <?php echo $_SESSION[‘session_variable_name’]; ?> such as <?php echo $_SESSION[‘session_name’]; ?>

So that’s it for today. Bye 🙂


About AnujAroshA

Working as a Senior Software Engineer. Specialized in iOS application development. A simple person :)
This entry was posted in PHP examples. Bookmark the permalink.

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