Re: Dynamic Display of Images Stored in DB

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



...

>>>
>> http://www.wellho.net/solutions/php-example-php-form-image-upload-store-in-mysql-database-retreive.html

having taken a quick look at that page I can only hope that you aspire
to write alot better code than that!

>>> Thanks very much for reading this long post.
>>>
>>> David
>>>
>>
> Thing is, the above works just fine as long as I hard code a switch case for
> every image file pulled from the db. What I can't seem to do is dynamically
> extend it to include newly uploaded images, i.e., for an image count greater
> than what I've hard coded. I thought maybe something like 'eval' might be
> used (I know, if eval is the answer, I'm asking the wrong question). 

no to eval(). period. I personally have never had any reason to use eval()
there has always been a safer more performant way of achieving whatever it was.

possibly someone on this list can give a realworld, valid example of using eval(),
although I probably wouldn't put money on it.

To be
> honest, I don't even know _why_ the above works because it seems that stuff
> is happening as if in a while loop when there isn't one (the switch case
> part).

I reread the code and I understand what it does now. I don't think it's very good.
to start I would split it up into 2 scripts. one to generate the HTML table and
one to output image data (basically what the switch statement does).

secondly when your building the html table you don't need to retrieve the image
data - change your select statement so that it is not included, that will be more performant.
the src attributes of the img tags should reference your new image output script something
like this

	<img src="img.php?im=$setID" alt="" />

lastly the new script you create to output image data should use an SQL query of it's
own to retrieve the image data relevant to the passed in id. something like this:

img.php:

<?php

$id = $_GET['im'];

if ($id && ($row = mysql_fetch_assoc(mysql_query("select imagedata from pictures WHERE pid=$id")))) {
	header("Content-type: image/jpeg");
	echo $row['image_data'];
}

exit;

?>

note I have not bothered to add much error checking of request variable sanitation ...
that is left as an exercise for the reader.

the whole example rests on the fact that the table containing the image data should
have a primary key that you can reference (so don't use some incremented counter for the
value of $setID !!!), additionally you will need to store the mime-type along with the
image data in order to be able to output the correct Content-type header.

one more thing - your a noob - noobs are not allowed (imho) to prefix expressions with the
error repression symbol (the '@' symbol) ... you want to see/log errors, there is hardly
ever any reason to use '@' to repress errors and if you really need it you will know
exactly why. sounds harsh but it will save you headaches.

> 
> For the moment, I'd be happy to just understand how it works as it does.
> Maybe as you suggest, there is raw image data somehow being embedded in the
> html.

no, nothing is being embedded - the script is being called in 2 different contexts,
firstly to output html, secondly to output image data - the script is incredibly
inefficient - every call to the script is retrieving all the image data from the database,
which hopefully you can understand is completely unnecessary.

> 
> On a peripherally related note, I did learn that syntax like <img
> src=?im=6> looks like this in the page source:
> <img src=http://www.mypage.php?im=6>.
> 
> Never liked the short-hand stuff...
> 
> Thanks, Jochem, for responding.
> 
> David
> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux