Jump to content

Getting and Displaying images from BLOB.....


exbrief

Recommended Posts

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

 

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

//If they did not enter a search term we give them an error
if ($_POST[find] == "")
{
echo "
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 "
";
echo $result['city'];
echo "
";
echo $result['state'];
echo "
";
echo $result['country'];
echo "
";
echo $result['gender'];
echo "
";
echo $result['main_pic'];
echo "
";
echo $result['pic_two'];
echo "
";
echo $result['pic_three'];
echo "
";
echo $result['pic_four'];
echo "
";
echo $result['pic_five'];
echo "
";
echo "
";
}

//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 \"$find\"yet.  
Please try again.

";
}

}

?>

Link to comment
Share on other sites

you can infact save images as blobs, I have seen it done in many a turorial online, and my script does it sufficiently, but I am having trouble getting them to be retrieved and displayed again.

 

I know that this can be done an alternate way by putting the images in a seperate folder then calling them up again with html tags. I am not sure how to do it this way.....

 

I have a form that people enter their name, city, etc and upload up to 5 pics. When you search with my search script you get the information....but not the pics. I need scripts that will do all of this.

Link to comment
Share on other sites

I know that this can be done an alternate way by putting the images in a seperate folder then calling them up again with html tags. I am not sure how to do it this way.....

 

Thats the way i do all my images on my social networking site with about 100k images.

 

$sql = "INSERT INTO `images` (`imagename`,`user_id`)VALUES ('" . $_FILE['filename'] . "', '530');";

 

That inserts the filename and the user_id.

 

And to retrieve simply do :

$sql = "SELECT * from `images` WHERE `user_id` = '530';
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
       echo "Image: 
";
}

 

That simply pulls the image from the same directory as the script but u can just put relative dirs in fron to get from other locations

echo "Image: 
";

Link to comment
Share on other sites

i know the actual procedure like you explained above. Here is what I am confused on, cant wrap my head around it.....

 

When you fill out my HTML form the info you enter is put into a table in my mySQL database. When you go search for say John Smith the search script finds john smith under the :name: field and then pulls that row from the table with all its supporting info (city, state, etc). It knows what to pull and print on screen because it is in the same row, so if I upload images to an outside folder, even if a link is created to the image uploaded in the form, does the search result just print that link, or does it go and get the actual image that the link refers to and display i? How does it know on multiple images.....just confusing to me, not sure why.

 

I guess I am not sure how this sets up the userID (e.g. 530 in your example above)...without me manually telling it to do this which will be impossible with thousands of users daily....and they enter all the info anyway.

 

Do you have a working script that allows all this to happen or know of one I can use and just edit to fit my needs?

Edited by exbrief
Link to comment
Share on other sites

use html with php to display the image:

 

<?php

 

$getresults = mysql_query("SELECT * FROM yourtable WHERE id = '$userid'");

 

while ($result = mysql_fetch_array($getresults)) {

 

?>

 

<? echo $results['imageurl']; ?>

 

<?php

 

}

 

?>

 

for multiple images you would do this in 2 ways;

 

a) if you have a set amount of images, you would have imageurl, imageurl2, imageurl3 etc... in your user table.

 

B) unlimitted images, you need two tables, users, images. you then link the tables eg:

 

images table would contain; id, imageurl, imageowner - with imageowner being the id of the user.

 

the query would then be

 

$getimages = mysql_query("SELECT * FROM images WHERE ownerid = '$userid'");

Link to comment
Share on other sites

your user table will have:

 

id (autonumber, primary key)

name

city

state

 

your images table will have:

id (autonumber, primary key)

imageurl (the location of image on server eg http://www.yoursite.com/images/image.jpg

ownerid (this is the id from the users table)

 

example user:

 

10

fred

orlando

florida

 

corresponding images:

 

1

http://www.yoursite.com/images/image1.jpg

10

 

----------------

 

2

http://www.yoursite.com/images/image2.jpg

10

 

Both images are owner by 10 which is the id for fred in the user table.

 

The image upload form will be:

 

 

 

 

 

 

 

 

 

you would have already assigned $userid earlier in your script eg:

 

$userid = $_COOKIE['userid']; // if your are using a login cookie for example.

Edited by sjhwebdesign
Link to comment
Share on other sites

that totally makes sense to me, thanks for that explaination. I have a search form that llows you to search the database by name, city, state, etc. The search pulls the info from the field that the searh matches, obviously, and returns the information in that field. What do I have to modify/add to make that script pull the image from the second table, and display it with the information? preferably main pic large and the others in a thumbnail.

Link to comment
Share on other sites

eg search by name:

 

<?php

 

$name = $_POST['name'];

 

$getresults = mysql_query("SELECT * FROM users WHERE name = '$name'");

 

while ($results = mysql_fetch_array($getresults)) {

 

echo $results['name'];

echo $results['city'];

echo $results['state'];

 

$userid = $results['id'];

 

$getimages = mysql_query("SELECT * FROM images WHERE ownerid = '$userid'");

 

while ($images = mysql_query($getimages)) { ?>

<? echo $images['imageurl']; ?>

<?php

}

 

}

 

?>

 

you would have to create your html layout to suit your webpage.

Link to comment
Share on other sites

in terms of 1 big main image and the rest as thumbs, you would need to tag one image as the main image.

 

this could be done by having the main image in the user table as a main image feild and the images table is the additional images.

 

or you could have a extra feild in the images table to tag the image order eg "theorder", and make the query:

 

$getimages = mysql_query("SELECT * FROM images WHERE ownerid = '$userid' ORDER BY theorder ASC");

Link to comment
Share on other sites

basically the point is to get the output to look like this when done...

 

so I am gonna try to do what you said, then if it works cool, if I cant get it to work I will be back, thanks.

 

http://www.ex-brief.com/test/example.png

 

 

 

NOTE: I am allowing only 5 images to be uploaded so I will add imageurl, imageurl2, etc to my existing table.

Edited by exbrief
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...