PDA

View Full Version : saving image in database


red_rose
01-14-2007, 12:18 PM
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.

davidj
01-14-2007, 12:49 PM
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

domedia
01-14-2007, 10:30 PM
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?

davidj
01-14-2007, 10:40 PM
Taken from http://www.onlamp.com/pub/a/onlamp/2002/07/11/MySQLtips.html (http://www.onlamp.com/pub/a/onlamp/2002/07/11/MySQLtips.html)

4. Don't store binary data in MySQL

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.

red_rose
01-15-2007, 02:53 AM
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

edbr
01-15-2007, 05:29 AM
It would seem to suggest not to store in the database. I dont so keeping the database itself smaller and easier to back up

davidj
01-15-2007, 07:29 AM
yes thats right

just save the image name in the table as a string

red_rose
01-15-2007, 12:29 PM
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

/////////////////////////////////////
$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));

?>

davidj
01-15-2007, 12:40 PM
<?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));

?>