Jump to content

Quick and simple way to display images from a database?


Moron

Recommended Posts

I never got this resolved some months ago, so I switched to using the First name and Last name from a database to construct a link to the images.

 

This presents two problems (so far):

 

1. I have to keep these images in a folder and make sure that the file names match the first and last names from the database.

 

2. We have two employees with the exact same first and last names. No kidding.

 

I've researched this a few times and it appears that I need two components for this to happen: one php file, like "image.php" or whatever that displays the image, and an <IMG> tag to point to this image.php file.

 

But I still can't get anything but a red 'x' so far.

 

Anyone know the correct way of doing this?

 

 

Link to comment
Share on other sites

I have no idea what you are trying to do. Could you explain it more, please?

 

If all you are trying to do is link an image to a database entry, why not just put a new field in the database with the image link? Or you could make a new table with the image links and link the two tables.

Link to comment
Share on other sites

You only need one image folder if you have the availability of a database. Alter the table that contains the first/last names to add an additional field for image name. Store the respective image name and use as you see fit. Just append image name to the image path. You can also test for anyone without an image using if ( empty() ) and display a temporary or 'No Image' image. People with same first/last names no longer matters, as each has a unique image filename in their database profile.

 

PhREEEk

Link to comment
Share on other sites

Here's the thing, guys, and maybe I didn't explain myself very well. Right now, I create a link to the employee's picture by using the first and last name from the database. For example, an employee named Fred Smith will have a link constructed that points to FredSmith.jpg and his picture will show. That's no problem.

 

But the reason I had to resort to this method is because I couldn't pull the image straight out of the database and have it show properly. When it shows at all, it only shows ASCII garbage. I don't want to have to maintain a folder full of employee images and constantly make sure that each file name is exactly FirstnameLastname.jpg and that it matches the database exactly. Not only that, but when I take someone's picture and he tells me that his name is Tom Jones, I don't want to have to browse to his picture and rename it ThomasJones.jpg a week later when he tells me that it isn't showing.

 

See what I mean?

 

I want to know the proper, working method, once and for all, to pull a binary image from the database and have it show as a picture.

 

Any help will be greatly appreciated.

 

EDIT: In case anyone is wondering, yes, I have the GD Library installed and working properly.

 

Link to comment
Share on other sites

//assume DB conn is made
$id = $_GET['id'];
if(!is_numeric($id)) exit;
$q = mysql_query("SELECT image FROM employees WHERE id = '{$id}'");
if($r = mysql_fetch_assoc) {
     header("Content-type: image/jpeg");
     echo $r['image'];
}

 

 

That does off course assume that your image is stored in a blob field as plain binary, and that the image was inserted correctly.

Link to comment
Share on other sites

//assume DB conn is made
$id = $_GET['id'];
if(!is_numeric($id)) exit;
$q = mysql_query("SELECT image FROM employees WHERE id = '{$id}'");
if($r = mysql_fetch_assoc) {
     header("Content-type: image/jpeg");
     echo $r['image'];
}

 

 

That does off course assume that your image is stored in a blob field as plain binary, and that the image was inserted correctly.

 

The storage works like this: I open up an Access database (yeah, yeah, I know) with an MS SQL back end. I click on the image block and paste the image into it (after resizing, etc....), then save. Then, of course, the php code queries straight from the SQL.

 

In your code posted above, would I need a connection statement on that page or is the connection statement on the page I call it from sufficient? And speaking of which, how do I call it? With an <IMG> tag?

 

Link to comment
Share on other sites

You're best bet is to do the following I had issues with images at first too. I don't know how many images you're trying to display at once but here's an example assuming you only want one image at a time.

 

First the previous code posted by corbin is stored in a file called view.php (or any other name you would like to call it but lets assume it's called view.php). Then you create a seperate page that has the following img tag in it.

 


<img src='view.php?id=1 title='some title' alt='some alt'><img />

 

And voila, should work.

 

 

 

Link to comment
Share on other sites

Ohhhh I assumed you were using MySQL....

 

I don't have much experience with Access, but I'm assuming Access is laying over MSSQL?  I didn't know Access and MSSQL could work together in that way....

 

<?php

error_reporting(E_ALL);

$cont = file_get_contents('untitled.jpg');
$cont = bin2hex($cont);
$q = "INSERT INTO images (image) VALUES (0x{$cont});";

mssql_connect('(local)', 'sa', '');
mssql_select_db('testdb');

mssql_query($q);

$q = mssql_query('SELECT TOP 1 convert(image, image) FROM images ORDER BY id DESC');
$r = mssql_fetch_row($q);
$h = fopen('untitled1.JPG', 'w');
fwrite($h, $r[0]);

?>

 

This successfully worked for me (MSSQL 2000).  it simply read a file and copied it to another location by inserting it to a mssql table and then selecting it.  That would show up in a web-browser if it had echo $r[0]; and header('Content-type: image/jpeg');.

 

Oh that's also with a varbinary field type....  Hopefully that will help.

 

 

Oh, by the way, here's an example of a page.  We'll call it image.php, and it would be called to like:

<img src="image.php?id=1" />

 

<?php

$id = (isset($_GET['id'])) ? $_GET['id'] : null;
if(!is_numeric($id)) exit;

mssql_connect('(local)', 'sa', '') or exit();
mssql_select_db('testdb') or exit();
//I'm not bothering with error handling since this is an example, and people would just see a red X for an image, and never the msg anyway
mssql_query($q) or exit();

$q = mssql_query("SELECT convert(image, image) FROM images where id = '{$id}'");
if(mssql_num_rows($q) != 1) exit;
$r = mssql_fetch_row($q);
echo $r[0];

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.