MrBool
You must be logged in to give feedback. Click here to login
[Close]

You must be logged to download.

Click here to login

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to select data in PHP

In this article we will learn how we can retrieve the images from the database using php and MySql.

[close]

You didn't like the quality of this content?

Would you like to comment what you didn't like?

Looking at different website that display different sort of picture or other media files are most- often store in database that is maintained by that website authorities. These pictures and other media files are dynamically displayed on the website using some specific scripts of Php. So now let’s begin our tutorials by first making a database in “xampp” or “wamp”, whatever sever or package you use.

You need to make a database and only one table in order to test this webpage. If you have navicate as front end the making database and table is very much easy just a task of few minutes.

Making a database with a name of mrBool

First of all make a database and the make a table with name of Emp that should contain the following Fields as described below.

  • ID type: int
  • Name ype: varchar
  • design type: varchar
  • image ype: int

After making the table as described above, the first thing we need is connection to the database we will create a separate file for this , since it is a good practice because during the development of a project when you want to connect many pages to the database in order to retrieve the data from there you can easily use the connection.php file which is further described below.

Making connection to MySql database

Make new file with name connection.php so that it may be included to other pages as per its requirement soon you will see below that where do we require this file just stick to the tutorial.

The file should contain the following line of code.

Listing 1: connection.php

<?php
$hostname_connection = "localhost";
$database_connection = "mrBool";
$username_connection = "root"; 
$password_connection = ""; 
$connection = mysql_connect($hostname_connection, $username_connection, $password_connection) or trigger_error(mysql_error(),E_USER_ERROR); 
mysql_select_db($database_connection,$connection) or die( mysql_error("could not connect to database! " ) ) ;
?>

Explaination to the code connection.php

Looking at the first line of code :

 $hostname_connection = "localhost";

This is the server name(assigned to variable) which is localhost since it runs on local machine.

Note: This sever name is changed at the time if you want to upload your website to any free or premium hosting sever because each hosting sever has it own different name that is for this particular purpose.

$database_connection = "mrBool";

This is the database name( assigned to variable), you can use any sort of variable name that is easy for you.

$username_connection = "root"; 

It is actually the user name (assigned to variable):

$password_connection = ""; 

The password (assigned to variable) that may be used by the login purposes its very much necessary when you are hosting your website here we give specific password for security purposes but while working on local machine (pc) we do not require the password .

$connection =mysql_connect($hostname_connection, $username_connection,$password_connection)or trigger_error(mysql_error(),E_USER_ERROR);

Now here looking at mysql_connect function that is used to connect with database, it takes three parameters server/hostname, username and password.

mysql_select_db($database_connection,$connection) or die( mysql_error("could not connect to database! " ) ) ;

Mysql_select is used to select the database it takes two parameters database name and connection variable in this case $connection.

Now our next target is to make a frontend that would display the information in the table.

We will be making a profile that gives information about the emp table along with a picture that is also stored in the database.

Listing 2: Index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MR.BOOltutorials </title>
<?phprequire_once('connection.php'); ?>
</head>

<body bgcolor="#c6c6c6">

<?php
$display ="select * from Emp";
$result=mysql_query($display,$connection) or die(mysql_error());
if($result == FALSE) {
die(mysql_error()); 
}?>
<h1 align="center"> Displaying displaying Information with picture </h1>
<table width = "245" border="0" align = "center">
<tr>

<td width = "50">Profile</td>

</tr>
<?php while($rows = mysql_fetch_assoc($result)){
	 ?>
<tr>

<td><imgsrc = "data:image/jpg;base64,<?php echo base64_encode(  $rows['image']); ?>" width = "100" height = "120" /></td>
</tr>
<tr>
<td><?php echo $rows['name'] ?></td>
</tr>
<tr>

<td><?php echo"Designation: ". $rows['desig'] ?></td>
</tr>
<?  } ?>
</tr>
</table>
< / body >
< / html>

Explanation to the code index.php

In this section we are going to explain the above code so the first line of code to be explained is:

<?phprequire_once('connection.php'); ?>

As you have seen we had made a php file with name of connection.php here we use require_once () function to include the file into our current file that is index.php. This is all done in order to connect with the database.

Our next php code lines are about how to apply query and fetch the result from database :

<?php
$display  = "select * from Emp";
$result = mysql_query($display,$connection) or die(mysql_error());
if($result == FALSE) {
die(mysql_error()); }?

Write a query and store it in any variable in this case we have $display, then using mysql_query function we run the query , this takes two arguments query holding variable($display) and connection variable($connection).After this we have just used if conditional clause to verify either the connection is made or not, the data is fetched from the database and saved in the variable $result. Now it is time to display it on the web-page, so let’s look at the next few lines of codes :

<table width = "245" border="0" align = "center">
<tr>
<td width = "50">Profile</td>
</tr>
<?php while($rows = mysql_fetch_assoc($result)){ ?>
<tr>
<td><imgsrc = "data:image/jpg;base64,<?php echo base64_encode(  $rows['image']); ?>" width = "100" height = "120" /></td>
</tr>
<tr>
<td><?php echo $rows['name'] ?></td>
</tr>
<tr>
<td><?php echo"Designation: ". $rows['desig'] ?></td>
</tr>
<?  } ?>
</tr>
</table>

We make use of the while loop and fetch out the data from the $result variable as an element of associative array and save them in the $row , it will continue until the records in the tables are not finished. Associative array in php uses key word in place of indexes. Now if you write echo $row[‘name’] ; it will fetch the name present in the table.

We used table and php code to display information that is set.

Note: For image to be retrieved from database you have to use this line of code :

<img class='imagem_artigo' src = "data:image/jpg;base64,<?php echo base64_encode(  $rows['image']); ?>" width = "100" height = "120" />

Now just with help of little css you can place this picture any where you want according to your desired location in different context.

Figure displaying the out Profile of Emp table.

Image Displayed

Figure 1: Image Displayed

Conclusion

So in this article we learned how to retrieve images from the database , this can be very usefull when we are dealing to keep profiles, with picture , and for making other web-application that use picture such as online book stores , photos related web-apps.

See also



computer science Graduate :Ceo at www.gigsclerk.com (Best Marketplace to Buy and sell service online from 2$-100$ ) and www.trustingeeks.com. Interested in programming, online buissiness. Android development.

What did you think of this post?
Services
Know how to keep MrBool Online
SUPPORT US
SUPPORT US
With your help, we can keep providing free content and helping you to be a better professional
support us
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually � in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits � in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login