Results 1 to 6 of 6
  1. #1
    Join Date
    May 2008
    Posts
    4

    Unanswered: Storing pictures in MySQL 5

    Hi all!
    I have a maybe simple question, but I haven't managed to find the answer.
    I tried to store some pictures in a MySQL database, that is the backbone of a site, and I used "BLOB" data type.When I made a PHP script to pull the pictures out (on the page),the result was text instead of pictures.So, I can't figure out,if I did something wrong in MySQL or in PHP.As far as I know,someone must use blob data type for pictures.The PHP script is establishing a MySQL connection and I use a simple "SELECT" statement to pull out pictures.If anybody could shed any light on this, I would appreciate it very much.Thanks in advance.

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    most people will tell you that it is far more convenient (and easy) to store the pictures on the server's file system, and store only the path to the picture in the database
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    And I'm one of them too! ^_^
    George
    Home | Blog

  4. #4
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    theres pro and cons to both techniques..
    personally I don't store images in a db, I use the path to the image.

    one thing you can do with PHP is, providing the images are on the same physical server, is use the GD linrary (IIRC) to get the image size
    I'd rather be riding on the Tiger 800 or the Norton

  5. #5
    Join Date
    May 2008
    Posts
    4
    Well, if I get it right, I must store in a table field the links to pictures and then in the PHP script, to pull each one out from db with a SELECT and with <img src="whatever the result of the SELECT is as path"> to display the image.But what I want to do, is not just pull some pictures of the webpage,but having the page design already made in a graphics editing program(like Photoshop for instance),to pull out this and after that to pull all the text I want,also from the database and with some CSS,to specify the page format.So,everything that belongs to every webpage must be pulled out from MySQL,and the template for all the web pages of the site is a PHP template.This way my resources as a whole,are kept in a safe place.I know that there is the simple way to have all graphics(pictures,web graphics,banners etc.),in a simple folder and pull them with simple <img src=...>,and the PHP scripts can do all the other things,but I prefer everything to be included via PHP from MySQL.I am almost sure that this has been done thousands if not millions of times,and I know that MySQL can do the trick with PHP,but there's something I'm missing.Thanks anyway for all the answers.As for storing picture links,I'll give it a shot...
    Last edited by tabexp; 12-30-08 at 14:24.

  6. #6
    Join Date
    Mar 2007
    Location
    010101010110100
    Posts
    803
    What you need are the "base64_encode" and base64_decode functions.

    encode the image before you save it to the db and decode it when you pull it down.

    I would recommend that you store only the link to the image in the database and store the actual images on the file system. Import the raw link from the db into your img tag. Don't store the img tag in the database.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •