Moron Posted December 17, 2007 Share Posted December 17, 2007 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? Quote Link to comment Share on other sites More sharing options...
lemmin Posted December 17, 2007 Share Posted December 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 17, 2007 Share Posted December 17, 2007 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 Quote Link to comment Share on other sites More sharing options...
Moron Posted December 18, 2007 Author Share Posted December 18, 2007 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. Quote Link to comment Share on other sites More sharing options...
corbin Posted December 18, 2007 Share Posted December 18, 2007 //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. Quote Link to comment Share on other sites More sharing options...
Moron Posted December 18, 2007 Author Share Posted December 18, 2007 //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? Quote Link to comment Share on other sites More sharing options...
dbillings Posted December 18, 2007 Share Posted December 18, 2007 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. Quote Link to comment Share on other sites More sharing options...
dbillings Posted December 18, 2007 Share Posted December 18, 2007 By the way the id=1 will require that you have an id stored in your database that actually equals one. You will probably want to replace that with a php variable to give you more flexibility. Quote Link to comment Share on other sites More sharing options...
corbin Posted December 18, 2007 Share Posted December 18, 2007 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]; ?> Quote Link to comment Share on other sites More sharing options...
Moron Posted December 18, 2007 Author Share Posted December 18, 2007 Aaargh. See my user name. I just can't get it to work. Thanks anyway, everyone. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.