logo-dw

Go Back   Dreamweaver Club Forums > Hand Coders Forum > General
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 01-14-2007, 11:18 AM   #1
red_rose
red_rose's Avatar
 
Join Date: Dec 2006
Location: kuala lumpur, malaysia
Posts: 69
Default saving image in database

hello there,

i would like to ask something on images in database. im developing a website, where there's a dropdown menu which redirects the user to a particular page when an option is selected. in that page, i would like to display information about a place which also displays an image. now, i don't know how can i save an image in the database (mysql) so that i can display it on the webpage. could anyone help me on this? thanks, will really appreciate it.
red_rose is offline   Reply With Quote
Old 01-14-2007, 11:49 AM   #2
davidj
davidj's Avatar
 
Join Date: Sep 2005
Location: The Toon (newcastle upon Tyne)
Posts: 8,256
Default

you can do this a couple of ways

1) you can store the binary data of the image in a BLOB in the db

2) you can store the path or name of the image in the db and call that value into an <image tag
__________________
Would you like to learn PHP from me? Check out -> www.codezenith.co.uk
davidj is offline   Reply With Quote
Old 01-14-2007, 09:30 PM   #3
domedia
Administrator
domedia's Avatar
 
Join Date: Dec 2003
Posts: 9,730
Default

Correct me if I'm wrong here David, but is it not considered good practice to store the path instead of storing the whole image in the DB? (If not I will have a serious talk with my programmers )

If me assumption is right, why is it considered a better practice?
domedia is offline   Reply With Quote
Old 01-14-2007, 09:40 PM   #4
davidj
davidj's Avatar
 
Join Date: Sep 2005
Location: The Toon (newcastle upon Tyne)
Posts: 8,256
Default

Taken from http://www.onlamp.com/pub/a/onlamp/2002/07/11/MySQLtips.html

Quote:
4. Don't store binary data in MySQL
Quote:

It is true that MySQL supports binary data types. Just because you can, however, does not imply that you should. MySQL sends results to a client all at once. Consequently, any application parsing a result set with binary data needs to wait for each row to arrive before it can be processed. Furthermore, there is no real benefit to storing the binary data in MySQL.
A better approach to binary data is to store that data on the file system and store pointers to those files in MySQL. With this approach, you can actually stream the binary data in background threads while you process the result set.
This tip does not apply only to binary data; it applies to any kind of large data objects. The performance issues that plague binary data also plague character data. In other words, the reading of any part of a result set is done serially. You will take more notice with binary data since it is usually large. You will notice the problems equally with large character data. You do need to weigh the performance benefits of storing large character data on the file system against the ability to search against that data in the database.
__________________
Would you like to learn PHP from me? Check out -> www.codezenith.co.uk
davidj is offline   Reply With Quote
Old 01-15-2007, 01:53 AM   #5
red_rose
red_rose's Avatar
 
Join Date: Dec 2006
Location: kuala lumpur, malaysia
Posts: 69
Default

so which method would you suggest me to use? this is first time im working with images in database, so that's why i need some guidance on this. thanks alot
red_rose is offline   Reply With Quote
Old 01-15-2007, 04:29 AM   #6
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,216
Default

It would seem to suggest not to store in the database. I dont so keeping the database itself smaller and easier to back up
edbr is offline   Reply With Quote
Old 01-15-2007, 06:29 AM   #7
davidj
davidj's Avatar
 
Join Date: Sep 2005
Location: The Toon (newcastle upon Tyne)
Posts: 8,256
Default

yes thats right

just save the image name in the table as a string
__________________
Would you like to learn PHP from me? Check out -> www.codezenith.co.uk
davidj is offline   Reply With Quote
Old 01-15-2007, 11:29 AM   #8
red_rose
red_rose's Avatar
 
Join Date: Dec 2006
Location: kuala lumpur, malaysia
Posts: 69
Default

i have tried writing a sample script to display an image from the db but failed. i saved the image in a folder named images and stored the image name in the db. could you please tell me what's wrong? thanks alot

PHP Code:
<?php
 
/////////////////////////////////////
$database  "services";
$username "root";
$password "sa";
/////////////////////////////////////
 
$link  = @mysql_connect('localhost'$username$password); 
$db  mysql_select_db($database$link);
 
$query sprintf("SELECT * FROM picture");
$result = @mysql_query($query);
$row = @mysql_fetch_array($result);

do {
echo 
"<img src="images/$row['pic_name']">";
}
while(
$row = @mysql_fetch_array($result));
 
?>
red_rose is offline   Reply With Quote
Old 01-15-2007, 11:40 AM   #9
davidj
davidj's Avatar
 
Join Date: Sep 2005
Location: The Toon (newcastle upon Tyne)
Posts: 8,256
Default

PHP Code:
<?php
 
/////////////////////////////////////
$database  "services";
$username "root";
$password "sa";
/////////////////////////////////////
 
$link  = @mysql_connect('localhost'$username$password); 
$db  mysql_select_db($database$link);
 
$query sprintf("SELECT * FROM picture");
$result = @mysql_query($query);
$row = @mysql_fetch_array($result);
 
do {
echo 
'<img src="images/'.$row['pic_name'].'">'// << changed here
}
while(
$row = @mysql_fetch_array($result));
 
?>
__________________
Would you like to learn PHP from me? Check out -> www.codezenith.co.uk
davidj is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 10:55 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2021, vBulletin Solutions, Inc.