Displaying Images from MYSQL using php

Please help, I really need a good simple example.

My setup:
Database is "images"
Table is "pictures"

fields are:
id is int
description is varchar
pic is blob

I need to get and display the image in a pure php page (.php) extension. I call this page from a seperate program:

run iexplorer.exe http://localhost/alarm.php$id=1

I don't want to reference a url in the database that points to the image. I want the image stored in the blob. ( different reasons) I am a newbie so please have mercy.
mratcliffeAsked:
Who is Participating?
 
Big_Red_DogConnect With a Mentor Commented:
First of all, the client side, you need to use "?" and not "$", http://localhost/doit.php?ID=1234

Server side, you printed out some text instead of just an image.  It you are going to put both the text and the image on the page together, then you need to generate HTML (you can put just text or just a picture out without HTML, but with both, you'll need to put out HTML.  It can be REALLY simple and you wouldn't use readfile() anymore.  I left everything in a pure php solution, but some of this would be easier outside of the PHP tags.

<?php
echo "<HTML><HEAD></HEAD><BODY>";
echo "Alarm Message for Tag, " . $ID;
$db = mysql_connect("localhost","admin","admin");
mysql_select_db("aam",$db);
$sql = "select * from alarm_messages where tagname = '$ID'";
$result = mysql_query($sql,$db);
$row = mysql_fetch_array($result);
$cause = $row["cause"];
echo "<P>";
echo "The Cause for this alarm is:" . $cause;
echo "<P>";
echo "The picture is: ";
echo "<BR>";
echo "<img alt='The error's image' src='" . $row['file_loc'] . "'>";
echo "</BODY></HTML>";
?>

0
 
Big_Red_DogCommented:
0
 
ezicoderCommented:
I assume you have picture in your MySql table...

You read that picture into buffer ex.:

$dPictureData = " ... jpeg content ..."

Your header should look like:

HTTP/1.1 200 OK
Server: XXXX
Date: Wed, 26 Feb 2003 09:11:04 GMT
Content-Type: image/gif
Accept-Ranges: bytes
Last-Modified: Thu, 20 Feb 2003 10:55:24 GMT
Content-Length: 745

// ok here is function how to download file use upper header in that function to show image. $sFileData can be your picture buffer
function DownloadFile_PutFile($sFileName, $sFileExtension, $sFileData)
  {
    $iFileLen = strlen($sFileData);    
    $datum = date("D, d M Y H:i:s")." GMT";
    header("Content-Type: application/$sFileExtension");
    header("Accept-Ranges: bytes");
    header("Last-Modified: $datum");    
    header("Expires: $datum");
    header("Content-Disposition: inline; filename=$sFileName.$sFileExtension");
    header("Pragma: no-cache");
    //header("Cache-Control: no-cache, must-revalidate");
    header("Cache-control: private");
    header("Content-Length: $iFileLen\n");    
    echo "$sFileData";
  }
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
KC_SpeedballCommented:
Big_Red_Dog's solution/hintis great... he solved it for me ;)
0
 
mratcliffeAuthor Commented:
Hi, I had read both of these before submitting a question.
The articles gave me good insight... but I still don't get it.

The senario is: A plant operator pics on a graphics button (seperate graphics package i.e. Foxboro I/A) This loads a variable about control point (a analog input).

The button also fires up iexplorer and passes the point variable i.e. ID to the .php page.

I use that variable in a where clause to query the db and bring up the logic picture to display to the operator. I want to do this in one .php file with no html files involved.
0
 
Big_Red_DogCommented:
In pseudo-code:

Let's assume when the button is pushed to pass the ID, it calls up doit.php and the ID is passed in as a query_string.  So the button's link is something like this: http://www.mysite.com/doit.php?ID=12345.  Of course if you are talking about a localized control machine, then you have to have a server with PHP configured into it running (probably apache) and you call it this way: http://127.0.0.1/this_controls_directory_under_htdocs/doit.php?ID=12345

--- doit.php ---
<?php
$sql = "SELECT * FROM mytable WHERE ID = " . $_GET['ID'];
$result = mysql_query( $sql );  // Do your error chaching, add your own "or die" etc. here...

// Assume we've stored the name of the file as column file_loc
$row=mysql_fetch_array($result);
$result = readfile( $row['file_loc'] );

// And now your picture is presented in your browser, no HTML, as long as it is a format the browser can display.
?>


0
 
mratcliffeAuthor Commented:
Yes I do have apache/php all running fine. Here is my do it.php file that gets the alarm text information. Now I just need to add to my select statement the field that stored the image and display it along with the text.

Client side http://localhost/doit.php$ID=1234

Server side: doit.php

<?php
echo( "Alarm Message for Tag, $ID" );
$db = mysql_connect("localhost","admin","admin");
mysql_select_db("aam",$db);
$sql = "select * from alarm_messages where tagname = '$ID'";
$result = mysql_query($sql,$db);
$row = mysql_fetch_array($result);
$cause = $row["cause"];
echo "The Cause for this alarm is:$cause";
?>

As you can see I am not real fancy. I really don't know
about some of your sytax i.e.   " . $_GET['ID']; or ($row['file_loc'] the but I am going to try.

If my picture name is pic_1.jpg then I'll use:
 
$result = readfile( $row['pic_1.jpg'] );

I'm not sure what you mean about the format part of the browser not supporting the image. I guess that is what the header section is supposed to take care of? I'm getting close and I really appreciate the response. I wish I had a real good book full of examples. I have been looking hard.
0
 
mratcliffeAuthor Commented:
Ok, I am close. Sorry about the $ instead of ?, my typo in the comment.

If I did use a mix of html and php (now assuming my file has a .html extension) how would I call up the html page from the client and pass it the ID value?

Thanks again.  I think your explainations are going to help a lot of people.
0
 
mratcliffeAuthor Commented:
Thank you much. I got it to work
0
 
Big_Red_DogCommented:
Glad I could help.  I am sorry I missed your additional question on 2/28.  EE notifications have been flakey lately and I didn't get an EMail that you had posted an additional comment.

Since you already got it to work I will leave a comment for anyone else who trips on thie Q with the answer to your 2/28 comment/question:

There are a couple of ways to do this.  The first is to rename the file to have a .php extension instead of .html with the ?ID=1234 query_string.  The second is to change your .htaccess file to recognize .html as a file type that must be processed by PHP.  In this case as well, you use the query_string ?ID=1234
0
 
mratcliffeAuthor Commented:
Thanks for the extra tidbit. When I was first trying it I had done the opposit. I took the .php file and renamed it .html....I didn't get far. Where is the .htaccess file located. I searched all through the apache and php directory.
0
 
Big_Red_DogCommented:
It is something you create yourself from scratch and put in the directory where your web pages are.  There are a lot of things you can control with the .htaccess file (if you are running on Wintel, you might use win.htaccess).  You can control access to directories.  You can control what error pages are displayed with the different errors, like 404.  You can control what file extensions are processed by which preprocessor, such as in this case.

See this for a good description of the htaccess file: http://apache-server.com/tutorials/ATusing-htaccess.html
0
All Courses

From novice to tech pro — start learning today.