quering db for each image

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

quering db for each image

Post by newmember »

i have an html page with links to images.
the images are located in inaccessable from outside location so for each link on above page some php page is being called that in turn sends the image to browser according to passed image id.

Code: Select all

<img src="sendimage.php?imgid=8" />
something that bothers me is that each time php page is called, it accesses
database according to image id(which is primary key) to retrive image storage.
(so if i display ,say 20 images on page, the browser will ask 20 times for images and that will demand 20 db queries.)

do you think that these kind of db accesses are bad for the running server?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Naw. If the query is just getting the filename for a given ID, it should be fine.

If this page starts getting accessed a lot (like 10+ times a minute),however, then the queries might start adding up and start affecting performance.

If this becomes a problem, maybe consider setting up a class or something to get the filenames for all the files the page is showing, then send those filenames to your sendimage.php file. That will cut down the queries from 20 times per page, to 1 time per page.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

only call the database once, then store the filenames/path in a session var.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I thought about that too (actually I was thinking an array), but he's calling them from the <img> tags spread throughout the page...not sure that would work.
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post by newmember »

ok thanks...
only call the database once, then store the filenames/path in a session var.
probably i'll do it this way...
Post Reply