Jump to content

Totally baffled on displaying image from Mysql db


simcoweb

Recommended Posts

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:

[code]// 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);[/code]

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:
[code]<?= @$eg_Result2['File1'] ?>[/code]

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

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 :)

Link to comment
Share on other sites

change this:

[code]
$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);
[/code]

to this:

$eg_Result2['
[code]
$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);
[/code]

let's see if you get an error and if so, what.
Link to comment
Share on other sites

Barand:

Ok, I changed this section to this:

[code]$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'];[/code]

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

Still no pic displayed.

Crayon, haven't tried your suggestion yet but will.
Link to comment
Share on other sites

This is what I get:

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

This is what I have for code:

[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;[/code]

Line 45 is the $sql=
Link to comment
Share on other sites

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:

[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);
}

}

}[/code]


Now, what I need is the proper coding to display the image in the HTML area. Thanks in advance.
Link to comment
Share on other sites

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:
[code]<image src="http://yoursite.com/getdbimage.php?id=1234">[/code]
-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:
[code]<image src="http://yoursite.com/getdbimage.php?id=<?php echo $id; ?>">[/code]
-that's all it takes
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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:
[code]<image src="http://yoursite.com/getdbimage.php?userid=<?php echo $primarykey; ?>">[/code]
--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. 

Link to comment
Share on other sites

'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:
[code]header("content-type: **jpg or gif or other**");
//use 'image/jpeg' for the content-type above for .jpg files[/code]
echo out the image field from the table record retrieved
[code]echo $dbrecord['image'];[/code]
-that's it!
Link to comment
Share on other sites

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

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

[code]<?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'];
?>[/code]

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.

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

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.
Link to comment
Share on other sites

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:
[code]<?php echo "<img src='http://www.plateauprofessionals.com/images/photo/$image' width='150' height='175'>"; ?>[/code]
-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.
Link to comment
Share on other sites

Yeah, as I explained originally I was storing the image [i]name[/i] 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
[b]photo[/b]

(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 [i]name[/i] 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. :)
Link to comment
Share on other sites

Just print out the value of that field like I said to see what is in it.  maybe the image name was entered wrong or didn't get entered at all.  If the image name does print out then create a full url from it and then enter it in the address field of your browser directly to make sure the image is on your web site and it is located where expected. It should be one of those problems otherwise one of the code suggestions given by the other posters should have worked.
Link to comment
Share on other sites

http://www.plateauprofessionals.com/images/photo/42-15602495.jpg is the direct link.

Also, I rewrote the 'getimage.php' so it would just display the entries in the 'File1' image field. There's only 3 entries in that field in that table. It displays the image names. Check it out:

http://www.plateauprofessionals.com/getimage.php

Here's the code for it. Excuse the sloppiness as i've made several revisions to it:

[code]<?php

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";
mysql_query($sql);
$Result = mysql_query($sql);

echo "<table>";
while ($a_row = mysql_fetch_array( $Result ))
{
  echo "<tr><td>" . $a_row['File1'] . "</td></tr>\n";
}
echo "</table>";

?>[/code]
Link to comment
Share on other sites

ok, then this should work:
[code]<?php
    echo '<img src="http://www.plateauprofessionals.com/images/photo/' . $image . '" width="150" height="175">';
?>[/code]
-replace $image with your db row field name and insert into your original page
-guess it would only actually work for 3 people in the db however

I entered the link into my browser
http://www.plateauprofessionals.com/images/photo/
-there's maybe 20 pix in that directory.  by the way, I shouldn't be able to do that, you should place an index.php file in that directory so I can't do that
Link to comment
Share on other sites

Not much hair left! This is getting really frustrating as i've tried SOOOOOOOO many angles on this.

Ok, here's the latest. First, here's the 'official' display page for the profiles and images utilizing some dummy people entered into the database. The first entry has an image file named in the 'imageName' field. The others don't. I'm using this tag to summon the pic:

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

You can view the page here: http://www.plateauprofessionals.com/display.php

For some reason it's displaying the ';? where the image should display. That's problem #1

Now, when I change the image tag to remove the <?php echo code and just have the following:

[quote]http://www.plateauprofessionals.com/images/photo/' .  $image . '[/quote]

Then the red x box shows but no image. So, if you check the properties (right click/properties) it shows this URL to the image:

[quote]http://www.plateauprofessionals.com/images/photo/'%20.%20%20.%20'[/quote]

I've even created a second sql query just for the images:

[code]$sql2=("SELECT imageName FROM members WHERE imageName='memberid'");
$result2 = mysql_query($sql2) or die(mysql_error());[/code]

And set a variable as: $image = $sql['imageName'];

No matter what i've done/do I can't get this simple friggin image to show up. Help?



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.