display image from path stored in MySQL

Posted on 2009-12-31
Last Modified: 2013-12-12
Hello and happy new year!

I have a a mysql database, a php-file and a html-file (attatched).

Question: How can I display the picture ? (look at my attatched files, then u will notice that I try to present the picture along with other data).

To specify my question more:

1) exctly whar data about the path to the picture needs to be stored in the database?Example; if the picture is named "youtube.gif" , do I only need to store the picturename in the sql-table ?

2) Secondly: What changes do I need to do in my php-script to display the picture ?

I need to solve this quicly. Please help :)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>


$dbhost = "";
$dbuser = "xxxxx";
$dbpass = "xxxxx";
$dbname = "xxxxxx";
	//Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
	//Select Database
mysql_select_db($dbname) or die(mysql_error());
	// Retrieve data from Query String
$age = $_GET['age'];
$sex = $_GET['sex'];
$wpm = $_GET['wpm'];
	// Escape User Input to help prevent SQL Injection
$age = mysql_real_escape_string($age);
$sex = mysql_real_escape_string($sex);
$wpm = mysql_real_escape_string($wpm);
	//build query
$query = "SELECT * FROM ajax_example WHERE ae_sex = '$sex'";
	$query .= " AND ae_age <= $age";
	$query .= " AND ae_wpm <= $wpm";
	//Execute query
$qry_result = mysql_query($query) or die(mysql_error());

	//Build Result String
$display_string = "<table>";
$display_string .= "<tr>";
$display_string .= "<th>Name</th>";
$display_string .= "<th>Age</th>";
$display_string .= "<th>Sex</th>";
$display_string .= "<th>WPM</th>";
$display_string .= "<th>Picture</th>";
$display_string .= "</tr>";

	// Insert a new row in the table for each person returned
while($row = mysql_fetch_array($qry_result)){
	$display_string .= "<tr>";
	$display_string .= "<td>$row[ae_name]</td>";
	$display_string .= "<td>$row[ae_age]</td>";
	$display_string .= "<td>$row[ae_sex]</td>";
	$display_string .= "<td>$row[ae_wpm]</td>";
	$display_string .= "<td>$row[ae_picture]</td>";
	$display_string .= "</tr>";
echo "Query: " . $query . "<br />";
$display_string .= "</table>";
echo $display_string;


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script language="javascript" type="text/javascript">
//Browser Support Code
function ajaxFunction(){
	var ajaxRequest;  // The variable that makes Ajax possible!
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('ajaxDiv');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
	var age = document.getElementById('age').value;
	var wpm = document.getElementById('wpm').value;
	var sex = document.getElementById('sex').value;
	var queryString = "?age=" + age + "&wpm=" + wpm + "&sex=" + sex;"GET", "ajax-example2.php" + queryString, true);


<form name='myForm'>
Max Age: <input type='text' id='age' /> <br />
Max WPM: <input type='text' id='wpm' />
<br />
Sex: <select id='sex'>
<option value='m'>m</option>
<option value='f'>f</option>
<input type='button' onclick='ajaxFunction()' value='Query MySQL' />
<div id='ajaxDiv'>Your result will display here</div>


Open in new window

Question by:granholmen
    LVL 21

    Expert Comment

    1. you may store a full path to the image along with image name.
    you may also store caption which can be altered later.
    depending on requirements you can extract image details and store it in to relative table.

    Check a very simple example below:
    LVL 21

    Accepted Solution

    that was for image stored in database: in case of path::
    considering your image is accessible from your domain, you may just print it as:

    ...display_string .= "<td>$row[ae_sex]</td>";
            $display_string .= "<td>$row[ae_wpm]</td>";
            $display_string .= "<td><img src='".$row[ae_picture]. "'></img></td>";
            $display_string .= "</tr>";

    Considered your $row[ae_picture]=

    Author Closing Comment

    Thank you so much for very quickly reponse and an accurate perfect answer!!!! You made my day!! :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API ( has made its way into the popular lexicon of the English language.  A few years ago, …
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now