PDA

View Full Version : install images into a table in my database


queenb
7-27-04, 07:42 AM
i have a file on my local computer with photos that i want to use for my website i want the photos to be coming from a table in my database. am new at this i really dont know how to do it. i also want the images to appear with transitions when they are loaded onto the website. please someone help i need a step by step explanation or tutorial i have been searching for days i cant find the solution please help Newbie

zimmer3
7-27-04, 09:02 AM
IMHO, to put images into a database, you need to have a field of the type BLOB, Medium BLOB, or Large BLOB. Search google for BLOB, image, PHP and you should find plenty of tutorials.

To get transistions you either need to ensure all visitors use Internet Explorer, or you need to write a JavaScript that willl also work in Netscape. I don't think PHP would have anything to do with this. Try DynamicDrive.com they might have a script for transitions.

Pig
7-27-04, 09:32 AM
You should not store images in your database. Very bad idea. Store then like relative files, and then store the paths to the images in the database.

zimmer3
7-27-04, 11:55 AM
You should not store images in your database. Very bad idea.

Why is it a bad idea?

blade83
7-27-04, 12:12 PM
an image is much greater in size than the path of an image...

you should not increase the size of your database unnecessarily...

storing the path to an image is just as useful as storing the image itself; when querying the database for the image, you can just use the path returned as the src of the image

zimmer3
7-27-04, 12:52 PM
I'm not arguing, but trying to understand.

As far as I can tell, a 20 k image takes 20 k of space whether in a database or a file in a folder. So you're not saving space.

MySQL offers these 4 different BLOBs (and their sizes)

TINYBLOB 255
BLOB 65,535
MEDIUMBLOB 16,777,215
LARGEBLOB 4,294,967,295
I can't see how 20 k could pose a problem for a system designed to store up to 4 Gig.

blade83
7-27-04, 01:03 PM
it doesnt save space in terms of overall disk space but does save space consumed by your database...

keeping a database small is an important priority

rather than sotring the file in the database thus increasing the size of the database, the file would be stored in your normal webspace with the path, a few bytes, stored in the database -- this will maintain database speed and efficiency

EDIT: basically, storing the image in the database unnecessarily increases the size of the DB because we can just store the image elsewhere -- its just a waste

Pig
7-27-04, 01:09 PM
There are 3 main reasons.
1) It is slower and less efficient because you must query the database and move through a tremendous amount of data (comparatively). Handling small strings is less instensive than handling entire images. It may not be a big deal when dealing with one 20 kb file, but imagine that difference iterrated over thousands of images, vs thousands of strings. The difference becomes very significant.

2) It is slower to get the image back out of the database, once you have found it.

3) This is really the most important reason, IMO. Backing up and restoring a database is problematic enough as it is. You will increase the size of your database exponentially by storing the images there. Any back up and restore process will likely time out, and you will have to perform such operations in small segments, instead of one shot.

queenb
8-2-04, 06:23 AM
thanks for your help i have managed to load the image path names into the database as you adviced but am having problems displaying them on the website can anyone help with the code am just having a red cross on the page no image displayed!

Pig
8-2-04, 08:18 AM
What code are you using to dislay them? It should be something like:

$sql = "SELECT image_path, image_name FROM image";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo($row['image_name'] . "<br> <img src='" . $row['image_path'] . "'><br><br>");
}

queenb
8-2-04, 08:25 AM
this is my code am not sure which image path i should use the one from my local pc or the funny one displayed in my database am new to this when i load another path name is displayed in my database am not sure what path to write after <img src=

$result = mysql_query("SELECT * FROM TABLEName ORDER BY ID");

if ($myrow = mysql_fetch_array($result)) {

echo "<table border=1>\n";

echo "<tr><td>Productcode</td><td>Name</td><td>Type</td><td>Category</td><td>Price</td><td>Picture</td></tr>\n";

do {



printf("<tr><td> %s</td><td> %s</td><td> %s</td><td> %s</td><td> %s</td><td>%s </tr>\n");
}
while ($myrow = mysql_fetch_array($result));
echo "<tr>\n";
echo "<td>".$myrow["id"]."</td>\n";
echo "<td>".$myrow["name"]."</td>\n";
echo "<td>".$myrow["type"]."</td>\n";
echo "<td>".$myrow["category"]."</td>\n";
echo "<td>".$myrow["price"]."</td>\n";
echo "<td><img src="images/Products/Wood/Animals/WoodLion.jpg " .$myrow["Picture "]."\" > </td>\n";
echo "</tr>\n";

}

zimmer3
8-2-04, 09:30 AM
echo "<td><img src="images/Products/Wood/Animals/WoodLion.jpg " .$myrow["Picture "]."\" > </td>\n";

Try changing to this

echo "<td><img src=\"" . $myrow["Picture "]."\" > </td>\n";

queenb
8-3-04, 05:38 AM
still not working

zimmer3
8-3-04, 07:23 AM
temporariliy comment out your echo statements, and try this

echo $myrow["Picture "];

and let us know what the result is. It should return the correct path for your image

queenb
8-3-04, 08:06 AM
maybe my code is all wrong still nothing is displayed i have been trying this for a week now am getting stressed

zimmer3
8-3-04, 08:38 AM
Hello,

I rechecked your initial code, and I find a line that does not seem correct
$result = mysql_query("SELECT * FROM TABLEName ORDER BY ID");
You should change the TABLEName to the actual name of your table, in which you have your images.

queenb
8-3-04, 08:43 AM
i have the actual name of the table placed where tablename is, i really dont understand whats wrong maybe am missing something . i have been on the net for hours trying to make it work.

zimmer3
8-3-04, 08:49 AM
Is this line in your code?
while ($myrow = mysql_fetch_array($result));

I think this should be
while ($myrow = mysql_fetch_array($result)){

queenb
8-3-04, 09:03 AM
it gives me this error
Parse error: parse error, unexpected '{', expecting ';'

Pig
8-3-04, 09:12 AM
You are using the do/while method, which is not used very much anymore. Even when it was, the only advantage is that it causes the loop to be executed at least once, even if there are no results to your query. You also have it implemented wrong. You should use a standard while loop.

Instead of this:

do {



printf("<tr><td> %s</td><td> %s</td><td> %s</td><td> %s</td><td> %s</td><td>%s </tr>\n");
}
while ($myrow = mysql_fetch_array($result));
echo "<tr>\n";
echo "<td>".$myrow["id"]."</td>\n";
echo "<td>".$myrow["name"]."</td>\n";
echo "<td>".$myrow["type"]."</td>\n";
echo "<td>".$myrow["category"]."</td>\n";
echo "<td>".$myrow["price"]."</td>\n";
echo "<td><img src="images/Products/Wood/Animals/WoodLion.jpg " .$myrow["Picture "]."\" > </td>\n";
echo "</tr>\n";


Try this:

while ($myrow = mysql_fetch_array($result))
{
echo "<tr>\n";
echo "<td>".$myrow["id"]."</td>\n";
echo "<td>".$myrow["name"]."</td>\n";
echo "<td>".$myrow["type"]."</td>\n";
echo "<td>".$myrow["category"]."</td>\n";
echo "<td>".$myrow["price"]."</td>\n";
echo "<td><img src="images/Products/Wood/Animals/WoodLion.jpg " .$myrow["Picture "]."\" > </td>\n";
echo "</tr>\n";
}

zimmer3
8-3-04, 09:16 AM
Pig's code above should still work, but the URL really looks Bad to me, because you are referencing a file that really appears like 2.

Pig
8-3-04, 09:17 AM
Now that I look at that, your img src is messed up. You have the path of a picture already in there, and then you are taking on another different path. That will never work.

You should put the full path of the images from htdocs into your database. So if your images are in domain.com/images/blah/, your database entries would look like images/blah/image1.jpg.

Then when you display them, you can use the document root.

echo("<td><img src='" . $_SERVER['DOCUMENT_ROOT'] . "/" . $myrow["Picture "] . "'></td>\n");

queenb
8-4-04, 05:25 AM
thanks for not giving up on me their is some improvement the problem is that where the picture is supposed to be theres just a box with a red cross what could i be lucking by the way in my table i dont have a blob type since you said it is not advisable am using a varchar could that be a problem or maybe under my picture field when i upload the picture it displays something like"/var/tmp/php3ehQjj" this could be the picture am really not sure what to do from here.
This may seem stupid but hey i cant find the htdocs directory where is it placed probably this is my biggest problem
thanks again for your continual help

Pig
8-4-04, 09:07 AM
Just to clarify, are you storing the pictures on your server (not in the database), and storing the path to the images in the database?

queenb
8-4-04, 09:46 AM
the pictures are on the server and path in the database. maybe the path is wrong this is the path /images/Products/Wood/Animals/WoodElephant.jpg when i load the photo it gives me the path /var/tmp/phpjore such funny stuff and i edit it with the path above could that be the problem

queenb
8-6-04, 05:02 AM
hey guys dont forget me am still stack with my problem i have noticed something everytime i load a picture i think it gives me the path to the internet temporal files could this be a guide to the solution of my problem?

queenb
8-6-04, 06:34 AM
am now bigginning to suspect that the problem should be the way i store the images in the database can someone check my code if its the correct way?

PHP CODE:
$result = mysql_query("INSERT INTO PRODUCTdisplays(PRODUCTcode,name,type,category,pri ce,picture,Availability)
VALUES ('$PRODUCTcode','$name','$type','$category','$pric e','$picture','$Availability')");
if (!result)
{
echo "it didnt work: !"; mysql_error();
exit;
}
else
{
print(" it worked!");




exec("cp $picture /full/path/to/PRODUCTdisplays/images/$picture_name");


echo "PRODUCTcode: $PRODUCTcode<br>\n";
echo "Name: $name<br>\n";
echo "Type: $type<br>\n";
echo "category: $category<br>\n";
echo "price: $price<br>\n";
echo "temp file: $picture<br>\n";
echo "file name: $picture_name<br>\n";
echo "file size: $picture_size<br>\n";
echo "file type: $picture_type<br>\n";
echo "<br>\n";
echo "<img src=images/$picture_name><br>\n";
}
?>

<br>Productcode<br>
<input type="Text" name="PRODUCTcode" size="25">
<br>
Name<br>
<input type="Text" name="name" size="25">
<br>
Type<br>
<input type="Text" name="type" size="25">
<br>
Category<br>
<input type="Text" name="category" size="25">
<br>
Price<br>
<input type="Text" name="price" size="25">
<br>
Picture<br>
<input type="File" name="picture" size="25">
<br>
Availability<br>
<input type="Text" name="Availability" size="25">
<br><br>
<input type="submit" name="submit" value="Upload">
</form>

IanS
8-6-04, 07:47 AM
am now bigginning to suspect that the problem should be the way i store the images in the database can someone check my code:

exec("cp $picture /full/path/to/PRODUCTdisplays/images/$picture_name");

This looks wrong. Let's clarify something because in an earlier post you asked which path to use also you've referred to the Temp internet files which are only on your local machine. So, when you uploaded your picutures, where are you putting them? You have a directory in your FTP root called

htdocs ? (You do, don't you?)

In that directory you have a sub-directory called

PRODUCTdisplays ?(Is that correct? - Remember that all of this will be case sensitive and 'productdisplays' is not the same)

In PRODUCTdisplays you have a directory

images ?(Is this the case?)

And in the images directory you have the pictures?

queenb
8-6-04, 08:29 AM
that is the problem i just got that code from somewhere and pasted tried to edit info.Those directories are wrong PRODUCTdisplays is a table on my database where i store the images they are served on the server am trying to get the images from a directory in my domain called images and upload them into a table called PRODUCTdisplays on my database then display them i think the problem for sure is the path i dont know how to insert the correct path in my database.

casbboy
8-7-04, 06:41 PM
Queen Bee, I am going to show you something I have setup that loads my picutes using an image path..... [for the sake of example, don't use the while loop and instead just pull one row from your table]... like so

this is with my table settings of course...

$news1 = mysql_query("SELECT * FROM headline LIMIT 0,1");
$fnews1 = mysql_fetch_array($news1);
//just grabbed one row from the table above and now put into an array
//put this on top of page above html tags


Ok, now lets say this page is opening from your index http://www.yoursite.com/index.php and your image folder is images... so http://www.yoursite.com/images/
Here is one thing you could do if you are putting all your images in the image folder...[and, to avoid error you could do a direct path like so]


$imagepath1 = "http://www.yoursite.com/images/" .$fnews1['image'];
//'image' is the name of the column with your image name
//notice now that $imagepath1 is the full, direct, path to the image
//also put this ontop of page above html tags


Now all we have to do is do something like this

<img src="<?php echo $realpath1; ?>">
//put this wherever you want the image
//to do with a loop like WHILE, just put the $imagepath into the loop


With this, all you need to do is put the image name within the database table, then the page will grab the name and create the path and get the image

HERE IS A LOOP EXAMPLE
this goes on top, above html

<?php
$henter = mysql_query("SELECT * FROM headline");
?>


i have this in my body

<?php
while ( $runenter = mysql_fetch_array($henter) ) {
$realpathenter = "http://www.canmag.com/images/" .$runenter['image'];

echo "<table width=\"500\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
<tr>
<td width=\"6\">&nbsp;</td>
<td><img src=\"". $realpathenter ."\" width=\"80\" align=\"left\" border=\"1\" class=\"feat-img\"><a href=\"getnews.php?id=". $runenter['newsid'] ."&issuenum=". $runenter['issue'] ."&department=". $runenter['field'] ."\" id=\"link-title\" class=\"bold\">". $runenter['title'] ."</a><br><font class=\"feature-abstrace\">". $runenter['abstract'] ."</font></td>
</tr>
</table>
<img src=\"images\transparent.gif\" width=\"50\" height=\"8\">";

}}
?>


So now it does a real direct image path for each image for each row, the other information I have is for puting out text I have in the row as well, yours could be different. to see my site that does loops with image paths goto http://www.canmag.com

Ryan

queenb
8-10-04, 05:58 AM
am getting more confused! anyway the whole aim of this is to display images of my products for sale and i checked the website ypu gave me and i saw how you display your posters i want to come up with something like that am i on the right direction all i need to use a photo gallery. if so how do i do that!
thanks for the help am very grateful to all of you

casbboy
8-11-04, 03:52 PM
Ok,

Since you can't get anything working... lets just get one image working.

Do this, give me the name of your table and the name of your column that contains your image path [maybe 'image' or image path] and I will write out the php more specifically. All you should have to do is copy and paste to try it out.

Also, do you have the file connecting to your database?? Or you just quering with no connection string?

most people do an

//YOU PUT THIS FIRST, above your queries,, connects you to database
include 'www/u/username/htdocs/connection/connect.db';
//or whatever your path is to the file with the connection variables.


Ryan

DontHurt
8-11-04, 05:18 PM
also, mysql stuff is stored in ascii/text (im not 100% sure but this seems to be the case especially with data dumps)
images are ment to be binary.

if you store binary as ascii you also run the risk of corrupting the images more.

also, think of it from a ligistical prospective
if the image is in the database, then there is alot more effort on the db server to send that image, whereas it's easy for a webserver to send that image.

bottom line, dont store images/files in a database, it's just stupid and asking for trouble later.

queenb
8-12-04, 06:29 AM
ok let me use simple language this is the first time am trying to do this so please bear with me. my website is hosted by powweb i created a mysql database my table name is PRODUCTdisplays the column is Picture. the path that i have right now is for the temporary internet files i want the images to be uploaded to this folder mywebsitename/images/product[/url] then in my table the column picture should contains a path to this folder and not the actual image.
hey i have just realised something i think my insert statement is wrong am inserting the image not the path so how do i insert the path

php code:

$result = mysql_query("INSERT INTO PRODUCTdisplays(PRODUCTcode,name,type,category,pri ce,picture,Availability)
VALUES ('$PRODUCTcode','$name','$type','$category','$pric e','$picture','$Availability')");

where theres $picture i think i need to insert the path.

hope you understand what am trying to say.check my earlier post for the full code.

zimmer3
8-12-04, 11:33 AM
hello,

the variable $picture should be defined earlier as the path of the image, such as

$picture="mypicture.jpg";

also, not sure if you copied wrong, but the word pri ce should be price, especially $pri ce.

Below should work for you

$sqlstatement = "INSERT INTO PRODUCTdisplays(
PRODUCTcode,
name,
type,
category,
price,
picture,
Availability
) VALUES (
$PRODUCTcode,
$name,
$type,
$category,
$price,
$picture,
$Availability
)";

$result = mysql_query($sqlstatement);

Good Luck

queenb
8-13-04, 06:58 AM
hey thanks for your help but still the image wont display maybe its the way i defined
$picture

$picture="http://mydomain/directory/imagedirectory/$picture_name
this is the path but still doest work.

queenb
8-17-04, 08:12 AM
have you all forgotten about this thread guys am now beggining to think the problem is with the browser coz all the help leads to nothing! am taking two days off to rethink the red cross box is still my friend doesnt want leave!

casbboy
8-17-04, 09:09 PM
Did you read my Private Message??

This is how the path should be, you need another quotation mark and a period

$picture="http://www.yoursite.com/images/". $picture_name;


You need a period to separate variables from entire texts like this.

$example="My name is ". $name ." and I am ". $age ." years old";


Check the private message, pretty setup.
Thanks
Ryan

DontHurt
8-17-04, 10:36 PM
not as an insult, but you dont seem to understand how HTML works let along PHP, and if that's the case it's really hard for you to jump into php/mysql if you dont know the underlining basics of it.


i recommend you get a php book or go to a php tutorial site, and try to understand that first before you write spaghetti code.

and you shouldnt blame the browser especially when you dont seem to understand how your code works.

casbboy
8-18-04, 12:03 AM
Good point, maybe that is where the confusion is. Do you understand html? Did you really make the path http://www.mydomain.com?? Or did you insert your information? Such as http://www.yahoo.com [this is yahoo's address, not yours, just for sake of example]

Ryan