Page 1 of 2

Images not loading...

Posted: Wed Sep 16, 2009 7:08 am
by WhiteHawksan
Well this code works when attempting to display a random image from the database, but when passed a variable (currently via session in a while loop) the image does not work. I can't see anything obviosuly wrong so if I'm being stupid or theres a bug I don't know about any help would be appreciated.

images.php

Code: Select all

<?php
    // just so we know it is borked
    error_reporting(E_ALL);
 
    include("connect.php");
    
    $i_id = $_SESSION['ImageID'];
        
    $sql = mysql_query("SELECT * FROM `tbl_images` WHERE `ImageID` = '$i_id' LIMIT 1") or die("Error handling Request");
        
        $res = mysql_fetch_array( $sql );
    // set the header for the image
    
        
    header("Content-type: image/jpeg");
    echo $res['Image'];
 
?>
Roughly the source

Code: Select all

<?php
//start the session
session_start();
 
include("connect.php");
 
$sql = mysql_query("SELECT * FROM `tbl_items`");
 
while($result = mysql_fetch_array( $sql ))
{
    $_SESSION['ImageID'] = $result['ImageID'];
 
    //some layout code etc.
    echo"<img src=images.php />";
}
 
 
produces a no image.

Any ideas??

Regards,
WhiteHawksan

Re: Images not loading...

Posted: Wed Sep 16, 2009 9:25 am
by Weiry
Do you have to pass the image name though a session variable without the use of $_GET?
because i know the way i currently have an image script, i use

Code: Select all

imagesource.php?id=1
as an example.
Also, im not sure if it would effect if or not, but have you checked if you are getting any $_SESSION variables in your images.php? I see you started a session in your test code, but not in your images.php. Without the inclusion of session_start() on each page you want session variables, you may lose the variables your passing. (not sure though)

Re: Images not loading...

Posted: Wed Sep 16, 2009 11:37 am
by WhiteHawksan
ok, jsut had a quick look by commenting out the header and echo of the image and just echoing the $_SESSION var and its fine, but still no images -.-

Re: Images not loading...

Posted: Wed Sep 16, 2009 11:54 am
by dude81
Check on this

Code: Select all

 
  $res = mysql_fetch_array( $sql );
 
I guess you need to have mysql_fetch_row

Re: Images not loading...

Posted: Wed Sep 16, 2009 12:02 pm
by superdezign
dude81 wrote:Check on this

Code: Select all

 
  $res = mysql_fetch_array( $sql );
 
I guess you need to have mysql_fetch_row
By default, mysql_fetch_array returns numerical and associative arrays. mysql_fetch_row only returns a numerical array.

Is the data in $res['image'] actual image data or just a file name? Saving the actual image data to the database is bad practice.

Re: Images not loading...

Posted: Wed Sep 16, 2009 12:21 pm
by WhiteHawksan
It is the request of the client to keep the image in the DB rather than as seperate file, and yes it does contain actual image data.

Re: Images not loading...

Posted: Wed Sep 16, 2009 12:29 pm
by superdezign
Okay then.
WhiteHawksan wrote:

Code: Select all

while($result = mysql_fetch_array( $sql ))
{
    $_SESSION['ImageID'] = $result['ImageID'];
 
    //some layout code etc.
    echo"<img src=images.php />";
}
Why would $_SESSION['ImageID'] be able to be changed from one to another? images.php will not run until after the line is output and the browser loads the script. By that time, $_SESSION['ImageID'] will likely be the id of the very last image. Then, all of the calls to images.php would be using the same session variable value. PHP is server-side and does not run at the same time as client-side operations.

Using the $_GET array is much more sensible.

Re: Images not loading...

Posted: Wed Sep 16, 2009 12:36 pm
by WhiteHawksan
the problwem is its multiple images being displayed in one load and I have check manually that both $i_id and $_SESSION[ImageID'] both have the correct values each time the page is called :S
at least when it becomes

Code: Select all

 
while(...) {
echo"<img src=images.php>";
include("images.php");
}
 
and the images.php is changed to

Code: Select all

 
</php
$i_id = $_SESSION['ImageID'];
 
echo"i_id: $i_id";
 
?>
 

Re: Images not loading...

Posted: Wed Sep 16, 2009 12:37 pm
by dude81
The reason could be before one image being loaded, the other image should be overwriting the image being loaded. The while code would finish the execution fast while images could be overwriting each of it. Thereby giving a completely different output.

Re: Images not loading...

Posted: Wed Sep 16, 2009 12:42 pm
by superdezign
WhiteHawksan wrote:the problwem is its multiple images being displayed in one load and I have check manually that both $i_id and $_SESSION[ImageID'] both have the correct values each time the page is called :S
No, I don't think you understood what I said. PHP is server-side, and thus finishes executing before output is shown in the browser (normally). For $_SESSION to have *anything* in it at all for a different page to access the contents, the session has to be saved. The session is not saved until the script is done running. Thus, images.php will only see the final value of $_SESSION.

Whether or not this is your current problem, it will be your future problem. Fix this first. Use $_GET and the query string.

Re: Images not loading...

Posted: Wed Sep 16, 2009 12:58 pm
by WhiteHawksan
-edit double post-

Re: Images not loading...

Posted: Wed Sep 16, 2009 1:00 pm
by WhiteHawksan
for now it makes no difference as NO images are displayed I'll pass the $_GET as I did orginally but thats a minor point as the value of $i_id is correct for each shop item it loads.

Just put it all back to $_GET but still no luck, all I get are the correct captions for the images with the same everything except that the image does not load (comes up with the little x in a box)

Re: Images not loading...

Posted: Wed Sep 16, 2009 3:06 pm
by superdezign
Does it work if you load images.php manually? Are you sure you are getting the data from the database? Have you checked it? Have you tried using the Content-Length (?) header?

Re: Images not loading...

Posted: Wed Sep 16, 2009 4:30 pm
by WhiteHawksan
ok now I'm confused on the $_GET :S I was passing it as ...src=images.php?id=$pic_id alt=... buuut that doesn't work :D

Re: Images not loading...

Posted: Wed Sep 16, 2009 9:55 pm
by Weiry
The only thing you should be passing if your using the $_GET method is the $pic_id.
so your image code would look like

Code: Select all

<img src='images.php?id=$pic_id' />
You dont pass all the image information.

Then in your images.php, you would have at the top of your page a variable that stores your $_GET (if you want to)

Code: Select all

$imageID = $_GET['id'];