PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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.
<?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'];
?>
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)
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.
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
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.
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.
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)
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?