Jump to content

MySQL / PHP images question.

- - - - -

  • Please log in to reply
8 replies to this topic

#1
Julien Duponte

Julien Duponte

    Newbie

  • Members
  • PipPip
  • 19 posts
Hello,
I'm a tad in over my head on this one, I think.
I'm not incredibly familiar with database programming and am working on an update.php file, that will allow my customer to add/edit entries for a "Used Equipment" page. The updater will allow them to upload an image, which I've decided to use a database for. My question, and forgive if it's ignorant, is do I need to make two seperate database files? One for the images and one for the text/description/etc. Or, can the images be stored in the same table with all of the text data? That would make my life easier, I assume. But, again, I'm at a loss on all this.
Many thanks!

#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
You can store images in the same table as other data (in a Blob field), but you may suffer performance issues if you're in the habit of typing SELECT * FROM...
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
If you were wishing to store an image name along side your other data, you can simply alter the table to provide this new column.

This would only be ran once.

ALTER TABLE TableName ADD ImageColumn VARCHAR(50)
You can then insert or update with this column as normal, along with your other columns of the query.

i.e.

UPDATE TableName SET ImageColumn = '$uploadedImageName' WHERE id = $idOfEquipment
You can then display their image by retrieving the image name from a SELECT query, and displaying:
echo "<img src='usedimages/$ImageFromQuery'/>";
A different, and slightly more difficult option is to store the entire image in the database opposed to just a name of it, a tutorial of this is located here:

http://forum.codecal...-mysql-php.html
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
Labram

Labram

    Newbie

  • Members
  • Pip
  • 2 posts
I suggest you use 2 separate databases.

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200

Labram said:

I suggest you use 2 separate databases.
Have you a reason why this is beneficial?
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#6
Julien Duponte

Julien Duponte

    Newbie

  • Members
  • PipPip
  • 19 posts
Thank you for your responses. I believe that I am going to be actually storing the images inside MySQL. Here is my confusion, I think. And, forgive, I have been pouring over PHP/MySQL tutorials, but they always address only one or the other, with regard to forms and upload pages. I would like to create an upload/edit page, that allows my customer to basically add an entry (almost like a news feed, I guess), as well as upload an image. I want the rows to all be tabled down the page, like they are here:
Northwest Tillers, Inc | NWTillers.com | Used Equipment

Can I upload a file, INSERT data (or UPDATE, if I create an edit option), all with the same SUBMIT button, then have it dynamically rendered on, say 'equipment.php'.

So far, how I am assuming that this will work, is that I'll have an update.php file, with the actual form,
that sends to a process.php file, that uploads/inserts the data into the database and forwards to an updated.php form, that just prints out if the upload/update was successful.
I am assuming/hoping, that then equipment.php will have been updating to look as it does now, but with this new feature.

I have some code that I'm working on, but I'm still having trouble wrapping my brain around it. I'm not wanting anyone to do my work, more just wanting to know if I'm just looking at the wrong tutorials/missing some functions/or entirely missing some steps. Thanks for the responses!
Matthew

#7
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
The tutorial I had linked to in the bottom of my first post should give you all you need to know, as it is only a simple use of the database.

To update or insert the image in to the actual database, you upload the file to a temporary directory as normal (I assume you have no problem with that), and then retrieve the file's contents with for example $contents = file_get_contents($tmpimglocation). These contents will be binary so you must apply addslashes() to them, so that they will insert correctly in to a database blob field. You should not use mysql_real_escape_string as that may work based on an encoding.

It will be accepted as a string, so if you in example name your blob field "imageblob", you will insert it like so:

INSERT INTO table (id, imageblob), ($id, '$escapedcontents');
Your image will simply be binary data in the database (BLOB is in fact Binary Large OBject), and to display it you must create a special page for that purpose to dynamically generate an image based off that binary data for the browser to read.

A very simplistic example of this page:
header('Content-Type: image/png');

$id = mysql_real_escape_string($_GET['id']);
$result = mysql_query("SELECT imageblob FROM table WHERE id = $id");
$row = mysql_fetch_row($result);
echo $row[0];
If you were to request id 12's image, you would access it simply like so:
<img src="display.php?id=12"/>
And the browser will render the result as an image, based on the ID you feed it.

Doing this is not too much different from working with strings so updating or inserting would be the same.

If you have any troubles on what to do where for this just ask!
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#8
Mark Wylde

Mark Wylde

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
Can I ask is there any reason you decided to store the image inside MySQL. I've never tried before but I would imagine you would come across some issues. When I'm creating a database I like to keep a little data as possible in there to make sure it keeps running nice and fast. Image files are much larger than a URL to an image located on a server, and with PHP you can upload a file to your server.

I'm just a little curious to know what the benefits to storing an uploaded file into a database is over file. Thanks!

#9
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
Database isn't that bad for performance, actually. Sure, it's larger, but it's normally on the same server, so it needs to be taken from somewhere, no matter what. One advantage of storing pictures in database is access control. If you store them in the database, you can, in the retrieval code, check access, which is impossible with direct file storage, as long as you have the URL to the file. So all have advantages and disadvantages.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users