Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
How to retrieve images from mySQL BLOB????
Old 02-20-2009, 12:33 AM How to retrieve images from mySQL BLOB????
Experienced Talker

Posts: 38
Name: carl
Trades: 0
ok, I have a form that people fill out that puts information (text) and up to 5 images into a mySQL database. The images are saves as LONGBLOBS in the database. This all works well.

Now, I have a script that allows you to search for records in the database, and it works great and displays the "text" information it retrieves from the Database fine....but I can not get the images to appear along with the information that does appear.

Can someone help me out modifying this code to get all the info, including images?

PHP SCRIPT

Code:
<?PHP
//This is only displayed if they have submitted the form
if ($_POST[searching] =="yes")
{
echo "<h2>Results</h2><p>";

//If they did not enter a search term we give them an error
if ($_POST[find] == "")
{
echo "<p>You forgot to enter a search term";
exit;
}

// Otherwise we connect to our Database

include "logininfo.php";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

// We preform a bit of filtering
$find = strtoupper($_POST[find]);
$find = strip_tags($find);
$find = trim ($find);

//Now we search for our search term, in the field the user specified


$data=mysql_query("select * from contacts where ".$_POST[field]." like '%".addslashes($find)."%'");

//And we display the results
while($result = mysql_fetch_array( $data ))
{
echo $result['first'];
echo " ";
echo $result['last'];
echo "<br>";
echo $result['city'];
echo "<br>";
echo $result['state'];
echo "<br>";
echo $result['country'];
echo "<br>";
echo $result['gender'];
echo "<br>";
echo $result['main_pic'];
echo "<br>";
echo $result['pic_two'];
echo "<br>";
echo $result['pic_three'];
echo "<br>";
echo $result['pic_four'];
echo "<br>";
echo $result['pic_five'];
echo "<br>";
echo "<br>";
}

//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, there is no Ex-Brief for <b>\"$find\"</b> yet.  
Please try again.<br><br>";
}

}

?>
Exbrief is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-20-2009, 02:23 AM Re: How to retrieve images from mySQL BLOB????
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
When you store the images in BLOBs, they are the contents of what would be an image file.
There is no direct way of outputting this, at least not yet.

It's much, much wiser to store the images in an uploads directory or whatever and store the filenames in your database.
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 02-20-2009, 02:27 AM Re: How to retrieve images from mySQL BLOB????
Experienced Talker

Posts: 38
Name: carl
Trades: 0
Quote:
Originally Posted by Insensus View Post
When you store the images in BLOBs, they are the contents of what would be an image file.
There is no direct way of outputting this, at least not yet.

It's much, much wiser to store the images in an uploads directory or whatever and store the filenames in your database.
I have seen "how-tos" showing how to store these as blobs then use <img> tags or something to that effect to output them as the original image, but I cant figure out how to make that work with my script. Also, I am not sure how to do this the way you say to do it either. Can you help me out on this?

How can I pull the database info on someone and the images from the outside file...how does it know what images to pull?? that is very confusing to me.

Last edited by Exbrief; 02-20-2009 at 02:29 AM..
Exbrief is offline
Reply With Quote
View Public Profile
 
Old 02-20-2009, 07:03 AM Re: How to retrieve images from mySQL BLOB????
phpdasan's Avatar
Experienced Talker

Posts: 37
Name: Karthick B
Trades: 0
Quote:
How can I pull the database info on someone and the images from the outside file...how does it know what images to pull?? that is very confusing to me.
Actually you are going too complex way rather than choosing the easiest way!! Storing images as BLOBs is space consuming and create unwanted coding headache as that of yours now...
Instead while inserting itself you can store the file name itself in a simple Varchar field, and upload file to a separate folder. Later when u retrive from database, get the filename and concate with the uploaded directory path to display the images using the <img> tag.

Hope this will help
__________________
There is no secret ingredient.
phpdasan is offline
Reply With Quote
View Public Profile
 
Old 02-20-2009, 08:19 AM Re: How to retrieve images from mySQL BLOB????
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
I have seen "how-tos" showing how to store these as blobs then use <img> tags or something to that effect to output them as the original image, but I cant figure out how to make that work with my script. Also, I am not sure how to do this the way you say to do it either. Can you help me out on this?

How can I pull the database info on someone and the images from the outside file...how does it know what images to pull?? that is very confusing to me.
You have to create a specific PHP script to display them.
Your problem now is that you need to send an content-type header to the browser to tell it what kind of image you have. jpg, png, or gif.
Now, if you don't already convert them at upload, you can convert them from an unknown type to jpeg at display time with PHP gd functions:
file "image.php":
PHP Code:
<?php
require("dbLayer.php"); //connection to the DB
$IMG_ID=$_GET['id']; //The image id to fetch

$query=<<<SQL
SELECT datas
FROM images
WHERE id=
$IMG_ID
SQL;
$res=mysql_query($query);
while(
$o=mysql_fetch_object($res)){
  
//output jpg version of the image
  
header('content-type: image/jpeg');
  
imagejpeg(imagecreatefromstring($o->datas));
  exit()
}
?>
Then, you can create img tags like this:
HTML Code:
<img src="image.php?id=12345" alt=""/>
and the image stored into your db will be automatically converted to jpeg and displayed

But I concur what both Insensus and Extbrief have said before me.
Storing images in the DB add overhead to the process.
If you don't need a distributed service (ie: several servers in a cluster working together) then storing the image in a given folder at upload time and storing the file name and/or path would be both simpler and more resource wise.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 02-20-2009, 05:24 PM Re: How to retrieve images from mySQL BLOB????
Experienced Talker

Posts: 38
Name: carl
Trades: 0
ok, so does anyone havve a script to do this easily, uploading images to a folder? thats a little confisung to me. It needs to be integrated into the database I already have which contains profile information (name, gender, city, state, etc...) of members...

Basically there is a form wherr members enter their info and I want them to be able to upload up to 5 images. Then when you search I need the info pulled and the images displayed. Not sure how the script will know what images to pull and display....here is where I am confused.
Exbrief is offline
Reply With Quote
View Public Profile
 
Old 02-20-2009, 07:30 PM Re: How to retrieve images from mySQL BLOB????
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Look at the example in the php manual, at the upload section
http://www.php.net/manual/en/functio...oaded-file.php
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to How to retrieve images from mySQL BLOB????
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.27821 seconds with 12 queries