Jump to content


Photo

Totally baffled on displaying image from Mysql db


  • Please log in to reply
34 replies to this topic

#1 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 10 September 2006 - 06:53 PM

Ok, summary:

I have a file called 'register.php' where person enters everything about their business plus sets username/password/confirmpass PLUS selects an image to upload ( a photo of themselves ). This all seems to work fine as all the data is being inserted properly (although it's not checking for dupes and allowing the same username to be added over and over..but that's another issue).

Now, I have page called 'members.php' that is to display their photograph and some other misc. crap. The key element is the photo. In the MySQL database in a table field is their photo's name. I have the query in the page to summon the table/field for it as so:

// Get Record Set
$eg_recResult2 = mysql_query("SELECT `plateau_pros`.`egnID`, `plateau_pros`.`File1` FROM `plateau_pros`  WHERE `plateau_pros`.`egnID` = '".@$eg_Result1['File1']."'", $eg_objConn1);
$eg_Result2 = @mysql_fetch_array($eg_recResult2, MYSQL_ASSOC);

The code i'm trying to use for calling and displaying the image in the page has ranged from using an echo statement to whatever:

tried this:
<?= @$eg_Result2['File1'] ?>

tried this:
<? echo "<img src='http://www.plateauprofessionals.com/images/photo/$image' width='150' height='175'>";?>


using this variable set: $image = ($eg_Result2['File1']['name']);

I know this is basic stuff but honestly I can not find any tuts on a simple explanation on how to properly do this. Most want to splash a bunch of code up there and say 'and this is how we do it'. If I had some explanations on the steps for each part of the code then I would never have to ask this question again. Seriously, i've ready 100 image upload-to-mysql/dispaly-from-mysql tutorials and none of them explain it...they just show it.

Anyway, I get lightning fast answers here which makes it the hottest php resource i've encountered. Any help IS ALWAYS appreciated :)



#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 10 September 2006 - 07:06 PM

change this:

$eg_recResult2 = mysql_query("SELECT `plateau_pros`.`egnID`, `plateau_pros`.`File1` FROM `plateau_pros`  WHERE `plateau_pros`.`egnID` = '".@$eg_Result1['File1']."'", $eg_objConn1);
$eg_Result2 = @mysql_fetch_array($eg_recResult2, MYSQL_ASSOC);

to this:

$eg_Result2['
$sql = "SELECT `plateau_pros`.`egnID`, `plateau_pros`.`File1` FROM `plateau_pros`  WHERE `plateau_pros`.`egnID` = '".@$eg_Result1['File1']."'";
$eg_recResult2 = mysql_query($sql, $eg_objConn1) or die(mysql_error() . "<b>  ~ query string:</b> $sql");
$eg_Result2 = @mysql_fetch_array($eg_recResult2, MYSQL_ASSOC);

let's see if you get an error and if so, what.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 10 September 2006 - 07:21 PM

<?php 
$image = $eg_Result2['File1'];
echo "<img src='http://www.plateauprofessionals.com/images/photo/$image' width='150' height='175'>";
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 10 September 2006 - 07:32 PM

Barand:

Ok, I changed this section to this:

$eg_recResult2 = mysql_query("SELECT `plateau_pros`.`egnID`, `plateau_pros`.`File1` FROM `plateau_pros`  WHERE `plateau_pros`.`egnID` = '".@$eg_Result1['File1']."'", $eg_objConn1);
$eg_Result2 = @mysql_fetch_array($eg_recResult2, MYSQL_ASSOC);

$image = $eg_Result2['File1'];

and my call for this pic to this:
<?php echo "<img src='http://www.plateauprofessionals.com/images/photo/$image' width='150' height='175'>"; ?>

Still no pic displayed.

Crayon, haven't tried your suggestion yet but will.


#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 10 September 2006 - 07:38 PM

Crayon, haven't tried your suggestion yet but will.

I recommend it
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 10 September 2006 - 07:45 PM

Ok, will do now but just a wee tad bit confused as to his suggestion. He says to 'switch to this' and places this:

$eg_Result2['


above his suggestion. Not sure what he's implying there. Clarification please?



#7 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 10 September 2006 - 07:46 PM

it was a typo. sorry. just use the part in the code block.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#8 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 10 September 2006 - 08:01 PM

This is what I get:

Parse error: parse error, unexpected ',' in /home2/wwwplat/public_html/members.php on line 45


This is what I have for code:

$sql="SELECT `plateau_pros`.`egnID`, `plateau_pros`.`File1` FROM `plateau_pros`  WHERE `plateau_pros`.`egnID` = '".@$eg_Result1['File1']."'", $eg_objConn1);
$eg_Result2 = @mysql_fetch_array($eg_recResult2, MYSQL_ASSOC;

Line 45 is the $sql=

#9 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 10 September 2006 - 11:20 PM

tiny bump :)

#10 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 10 September 2006 - 11:25 PM

You are defining the query string, but at the end you add ", $connection_resource" as though you were calling mysql_query(), which incidentally, is missing from the code.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#11 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 12 September 2006 - 09:06 PM

Ok, I'm switching gears here. All I want to do is provide the easiest method of uploading and storing an image then displaying that image in an HTML page. I have the script uploading the image no problem. The part that's causing the problem is displaying the image. Here's my upload code:

// Upload File
$eg_success_photo = false;
if(!empty($_FILES['photo']['name']))
{
	// Check file is not larger than specified maximum size
	$eg_allowUpload = $_FILES['photo']['size'] <= 100000 ? true : false;
	// Check file is of the specified type
	if($eg_allowUpload)
		$eg_allowUpload = preg_match('/\\.(gif|jpg|jpeg|png)$/i', $_FILES['photo']['name']) ? true : false;
	
	if($eg_allowUpload)
	{
		if(is_uploaded_file($_FILES['photo']['tmp_name']))
		{
			$eg_uploaddir = $_SERVER['DOCUMENT_ROOT']."/images/photo/";
			
			$eg_uploadphoto = $eg_uploaddir.rawurlencode($_FILES['photo']['name']);	
			// Create a unique filename for the uploaded file
			$eg_i = 1;
			while (file_exists($eg_uploadphoto))
			{
				$eg_separated_filename = explode(".",$eg_uploadphoto);
				if (substr($eg_separated_filename[0],-1) == $eg_i)
				{
					$eg_separated_filename[0] = substr($eg_separated_filename[0], 0, (strlen($eg_separated_filename[0])-1));
					$eg_i++;
				}
				$eg_separated_filename[0] = $eg_separated_filename[0] . "$eg_i";
				$eg_uploadphoto = implode(".",$eg_separated_filename);
			}
						
			$eg_success_photo = move_uploaded_file($_FILES['photo']['tmp_name'], $eg_uploadphoto);
		}
		
	}
	
}


Now, what I need is the proper coding to display the image in the HTML area. Thanks in advance.

#12 mainewoods

mainewoods
  • Members
  • PipPipPip
  • Advanced Member
  • 685 posts
  • LocationMaine

Posted 12 September 2006 - 09:19 PM

what you want to do is create a new .php page just to retrieve the images then use that php page in your image tag, like:
<image src="http://yoursite.com/getdbimage.php?id=1234">
-the getdbimage.php will just retrieve the id from its url and use that to get the proper db image and then just do a header(''Content-type: ????") for the image type and then just echo the graphic: echo dbrec['image'];

In the image tag above you would have to write the id# in dynamically in the original page like:
<image src="http://yoursite.com/getdbimage.php?id=<?php echo $id; ?>">
-that's all it takes

#13 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 12 September 2006 - 09:38 PM

Ok, I think I understand that. But here's the additional explanation that needs to be relayed to you before we settle on a course of action.

This is a 'profile' page that the image will be displayed on. Basically a photo of the individual. When a visitor to the site wants to view the profiles they would first click on a category which then displays then profiles within that category. Then select an individual from the list and display/view their entire profile. Kind of like how a dating site would work only this isn't a dating site. It's more like a directory of people providing professional services in 4 main categories.

So, when it displays the profile it has to display the image. If i'm understanding your suggestion correctly then when they clicked on the link in the listing page it would need to pass an ID variable to the full profile page where the 'getdbimage.php' file would pick it up?

I'm not 100% on how to 'connect' the uploaded pic to the person's profile so it would pass that info along.

#14 mainewoods

mainewoods
  • Members
  • PipPipPip
  • Advanced Member
  • 685 posts
  • LocationMaine

Posted 12 September 2006 - 09:49 PM

I see, you're saying that the picture is part of the table row that has the profile in it.  Use the key of that record in the code I showed you:
<image src="http://yoursite.com/getdbimage.php?userid=<?php echo $primarykey; ?>">
--your page outputs that image tag to the browser with the userid written into the url.  When the browser parses the page, it calls that image tag specifying a php page with a url parameter in it.  That php page called through the image tag only returns the image from the database for the userid specified.  That image is then displayed in place wherever you put the image tag in the original page. 



#15 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 12 September 2006 - 10:12 PM

Ok, cool. Then what's the code needed in the 'getdbimage.php' file?

#16 mainewoods

mainewoods
  • Members
  • PipPipPip
  • Advanced Member
  • 685 posts
  • LocationMaine

Posted 12 September 2006 - 11:07 PM

'getdbimage.php'  does this:
-connects to the db
-gets the url parameter of 'userid' passed to  it
-creates an sql statement using that parameter
   $sql = "SELECT imagefieldname from tablename WHERE userid='" . $userid . "'";
-execute that sql
-retrieve the record
-do a header for the content-type:
header("content-type: **jpg or gif or other**");
//use 'image/jpeg' for the content-type above for .jpg files
echo out the image field from the table record retrieved
echo $dbrecord['image'];
-that's it!

#17 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 12 September 2006 - 11:47 PM

Ok... garoooovy! I'll work it up and see if it works. If it doesn't...i'll be back! Thanks!

#18 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 13 September 2006 - 02:49 PM

Of course I can't get it to work :(

Here's the code I wrote for the 'getimage.php' file:

<?php
header("content-type: 'image/jpeg'");
include 'config.php';
mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());

$sql = "SELECT File1 FROM  plateau_pros WHERE egnID='".$userid ."'";
mysql_query($sql);

echo $userid['File1'];
?>

I've tried a variety of <img src tags but here's what I have there now. The 13 is the id of the member.

<image src="http://www.plateauprofessionals.com/getimage.php?id=13">

I know i'm missing something. All that shows is the broken image icon (red x) and when I right click on that and hit View Image it tries to download the getimage.php file. Any guidance is greatly appreciated.

#19 mainewoods

mainewoods
  • Members
  • PipPipPip
  • Advanced Member
  • 685 posts
  • LocationMaine

Posted 13 September 2006 - 03:58 PM

oops..,
sorry I seem to have lead you astray, I misread your original post and thought you said the image was stored in the database but you said the 'photo's name' was!  Which means I gave you the wrong method. You need to do some version of this:
<?php echo "<img src='http://www.plateauprofessionals.com/images/photo/$image' width='150' height='175'>"; ?>
-debug the code, buy inserting a php statement at the point of the image code above in the original file that just prints out the value in the db field so we can see what it is:
<?php echo "filename: " . $Result['File1']; ?>
if an actual file name print outs, then create a full url from it and enter it directly into the address field of your browser to see if the photo comes up.

-the extra php page I gave you is not necessary, because you are only storing the image name, not the image, sorry.


#20 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 13 September 2006 - 04:11 PM

Yeah, as I explained originally I was storing the image name in a MySQL database field as part of the person's profile. So, basically the table fields are:

id (auto incremented)
name
email
address
city
state
zip
business
title
cell
photo

(or, in this case the photo field is labeled File1)

The file upload function basically is parking the uploaded photo in to a folder /images/photo while the MySQL query upon submitting the registration form is inserting the name of the file into the table field. The file upload and data insertion are all included in the one 'register.php' script.

So, at this point i'm simply trying to get that damn picture that's sitting in the /images/photo folder to show up when someone wants to view 'X' profile. :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users