Skip to main content
GameDev.net gamedev.net
🔒 Locked

[web] Display images from sql via php

Started by Uthman Oct 18, 2005 at 11:04 AM 5 replies 1.6k views
Original Post
Uthman
Uthman
100% of the tutorials I've seen for displaying images from sql database basically are some php page that have the <> link where the link is another php script which goes ahead and logs into the db and handles setting the headers and sending the picture. Is there any way that the sepertate 'imageloader' script can be nested in the original php script (the script with the <> tag) so that I just have to log onto my db once instead of making 30 billion connections just to load one page with many images?
"a low level aho master like you couldn't kill me even if I let you"
Cygnus_X
Cygnus_X
Can you explain your setup a little more? I'm guessing you have something similar to the following:

Database Table

<1>
<2>

etc... and you're wanting to change what pics are displayed based on what theme the user has chosen (or something similar)? In other words, if theme 1 is chosen, pic1.jpg will be shown where as if theme 2 is chosen, pic2.jpg will be displayed.

If the above is true, you can always put different photo groups in different folders. For example theme 1 would be in location pictures/theme1/ where as theme 2 would be in location pictures/theme2/, etc... and all you'd need to do is call the database once to figure out which folder to pull from (given you choose a good naming convention for your pictures).
Uthman
Uthman
well, in the database, i have a table

tinyint id
mediumblob picture
int size
varchar filename
varchar type

i have a seperate php file - getimg.php

getimg is passed an id via post, getimg.php?id=1

getimg then proceeds to logging into the database, looking the picture that corresponds to the passed id, then sends the correct headers based on the image type, filename, size, etc, and sends the picture

header("Content-length: $size");header("Content-type: $type");header("Content-Disposition: attachment; filename=$filename");echo $picture;


from any browser window, i can type in getimg?id=## and it will display a picture.


however, I have a seperate page called showmygames.php in which i showcase a bunch of the games ive made. all of the games information is also stored in a table so I am already logged into the database inside of showmygames.php. when i want to display a game's iamge inside of showmygames, i have to:

<img src="getimg?id='$gameid'">.


i was interested in knowing if there was a way i could, from inside of showmygames.php, retreive the picture information using the current connection to the sql db instead of calling getimg so many times for each game that is displayed?
"a low level aho master like you couldn't kill me even if I let you"
Cygnus_X
Cygnus_X
Ah, I understand now.

That is a tough one. The way I'd try to do it (and I've never really used blogs before), is to say:

$Result = mysql_fetch_array("select * from PictureDataBaseTable where Criteria = '$Criteria'");

That will save all the pictures you want in $Result, and then you can close the connection. To get the first blog, do:

$Temp = mysql_fetch_array($Result); //fetches the array
$Picture = $Temp['PictureBlog']; //stores the blog data inside $Picture


So now you have a variable that stores the blog. You no longer need the database connection, but you would need to do some sort of loop to get all the pictures. From there, it would be ugly, but you 'could' do:



Then inside of the showpic.php file, you'd have something like:

global $HTTP_GET_VARS;

$Picture = $HTTP_GET_VARS['pic'];

Then run your 'display picture code', passing headers and stuff as need be. Like I said, it would be ugly, but.... you'd never see the uglyness unless you looked at the source code. The only problem is, I'm not sure if you can pass that large of variable in a url... I'd be interested to know if it works.

Hopefully that helps?
Uthman
Uthman
haha, well

if i send the image size and type, I could understand it working, but then i'd be doubling my bw, hehe

this is the page as it stands now (using the first technique)
"a low level aho master like you couldn't kill me even if I let you"
Cygnus_X
Cygnus_X
Well, what if instead of passing the variable via the url, you made it a global variable? Get all the pictures you want up front via the mysql call, then loop through each picture, make it global, then reference that global variable in your image script that sends the headers?

Unfortuantly, I havn't used global variables in php before in that sense. So... thats about all I've got.

intransigent-seal
intransigent-seal
Look into persistent database connections. (e.g., if you're using mysql, look at the documentation for the mysql_pconnect() function)

John B
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.