Jump to content

storing images in database

- - - - -

  • Please log in to reply
6 replies to this topic

#1
ravi951

ravi951

    Newbie

  • Members
  • PipPip
  • 18 posts
hi all,
i want to store an image in MYSQL database.please tell me whether the below sql syntax i have written is correct or not.

CREATE TABLE IF NOT EXISTS `products`(

  `serial` int(11) NOT NULL auto_increment,

  `name` varchar(20) collate latin1_general_ci NOT NULL,

  `description` varchar(255) collate latin1_general_ci NOT NULL,

  `price` float NOT NULL,

  `picture` varchar(80) collate latin1_general_ci NOT NULL,

  PRIMARY KEY(`serial`)

) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=7;

and i have inserted the values for the above table also.
below is the one.....

INSERT INTO `products` (`serial`, `name`, `description`, `price`, `picture`) VALUES

(1, 'View Sonic LCD', '19" View Sonic Black LCD, with 10 months warranty', 250, 'images/lcd.jpg'),

(2, 'IBM CDROM Drive', 'IBM CDROM Drive', 80, 'images/cdrom-drive.jpg'),

(3, 'Laptop Charger', 'Dell Laptop Charger with 6 months warranty', 50, 'images/charger.jpg'),

(4, 'Seagate Hard Drive', '80 GB Seagate Hard Drive in 10 months warranty', 40, 'images/hard-drive.jpg'),

(5, 'Atech Mouse', 'Black colored laser mouse. No warranty', 5, 'images/mouse.jpg'),

(6, 'Nokia 5800', 'Nokia 5800 XpressMusic is a mobile device with 3.2" widescreen display brings photos, video clips and web content to life', 299, 'images/mobile.jpg');

the "picture" field stores pictures in database.i have given there VARCHAR(80)
i know for storing pictures we should give BLOB data type.
but tell me whether the above syntax i have written will work or not.
if not what would be the syntax...

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
If you want the file, rather than a reference to the file, stored, you have to use a Blob type. Period.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
ravi951

ravi951

    Newbie

  • Members
  • PipPip
  • 18 posts
then do u mean that the sql query i have written is wrong.
then it will not display an image in output.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
It depends on what the other code will be doing. I know people who store files in a file share, and use the file path to locate it and show it. I know other people who store the file in the database, and return the file from the database to display. It all depends on the surrounding code.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
So how do you upload a file to a database?


I mean, to save a filename: UPDATE `table` SET filename='the_filename.ext' WHERE id=12

But what about a file? You wouldn't try to fit the contents of the file inside those single-quotes, would you? Even if you would, isn't there a better way?

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
The easiest way to do it usually depends a little on the language, but you'll usually use query parameters.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
yassinebelkaid

yassinebelkaid

    Newbie

  • Members
  • PipPip
  • 20 posts
Hello ravi951 :

what you did is alright, you display images from the path in your database, I came across with something like that, and it worked fine, as long as you put just the path of the picture varchar(80) will work fine. you can make a file type to upload the images, but don't forget to give it the same path in the database, so that you can fetch images from database and put them in your php pages.

Edited by yassinebelkaid, 21 August 2011 - 02:27 PM.
misspelling





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users