Inserting image into database..

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
187skillz
Forum Commoner
Posts: 39
Joined: Sun Aug 10, 2003 8:35 pm
Location: London

Inserting image into database..

Post by 187skillz »

Pls excuse my amateurish questions, I'm trying to make sense of this....ok

I created a table in my database

Code: Select all

create table items 
( 
itemId int auto_increment not null, 
itemName varchar(50), 
itemDesc varchar(250), 
itemPrice decimal(4,2), 
primary key(itemId), 
unique id(itemId) 
);
After that I inserted the code below.

Code: Select all

insert into items values(0, 'Tony Hawk 3', 'Tony Hawk is back. Join him in this popular skating game where speed, collisions and tricks come together to produce the best skating game of all time!', 23.95);
and another 2 code to produce this

http://www.ukbestkept.com/products.php


I need another column where I can add an image for my item.

how do I do that pls?


Also do I upload the image to my database? I've got phpmyadmin.

Thanks, I hope I made some sense and you're not lost.
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 »

Most people are going to tell you not to upload the image right to the database. It's much faster to just upload the location of the image into the database and store the image in the filesystem.

That said, if you still want to store the image in the database, you're going to need to make a column of type blob (mediumblob ideally). Then, when you want to insert the image, read the file in as a binary string, then store that string in the database. To display the image again, read the string back, then dump it to the screen after sending header("Content-type: image/jpg"); (or whatever file type it is).

It's certainly easier to use the first method - I'd suggest going that route.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
187skillz
Forum Commoner
Posts: 39
Joined: Sun Aug 10, 2003 8:35 pm
Location: London

Post by 187skillz »

pickle wrote:Most people are going to tell you not to upload the image right to the database. It's much faster to just upload the location of the image into the database and store the image in the filesystem.
.
Thanks and how do I alter my database to accomodate a table field for the image location pls?

I've got phpmyadmin, I would like to have the sql queries to do that. Thanks.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

just add a varchar field. i would do varchar(100) or somthing, maybe even up to 255 since I have seen people name their pictures with insaily discriptive names.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Are you going to be manually uploading the images?


Also to display the images you need to create a script, let's call it displayimg.php for example

when you output your page you select the image filename from the database and then output like this:

Code: Select all

<img src="displayimg.php<?=$filename?>" />

PHP would then read the image off the file-system, this way you aren't giving out the URL to your images. Also if you do this it's going to be easier some day to make modifications, like watermarking your images or only allowing members to see certain images for example
Post Reply