Jump to content

Storing pictures in an mysql database

D_lete

hi,

 

i'm doing a school project in which users can store pictures and text online for themselves. An online diary if you will. The text storing is great, works like a charm but now we want to store pictures as well. do you guys have any idea how to do this?

 

grtz

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Use DropBox?

we need to send aditional data with the picture, userID and postID, that way we can link the pic's to their user and posts

Link to comment
Share on other sites

Link to post
Share on other sites

Store the picture as a normal image on the server and store the path to the image in the database.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Store the picture as a normal image on the server and store the path to the image in the database.

that's the thing, we only were provided with an mysql server, i've just sent a request for an ftp server. i'm thinking if we name the pic the postID it 'll all work.

Link to comment
Share on other sites

Link to post
Share on other sites

we need to send aditional data with the picture, userID and postID, that way we can link the pic's to their user and posts

Store the paths/urls in the database. You will find that storing the images themselves in the database is much more expensive.

 

If you need to send aditional information then encode that data using XML/JSON in your applicaton and send it with the image URL/path.

Link to comment
Share on other sites

Link to post
Share on other sites

Store the paths/urls in the database. You will find that storing the images themselves in the database is much more expensive.

 

If you need to send aditional information then encode that data using XML/JSON in your applicaton and send it with the image URL/path.

if i get an ftp from school, which is very likely, would my idea of naming the pics the postID not work?

Link to comment
Share on other sites

Link to post
Share on other sites

if i get an ftp from school, which is very likely, would my idea of naming the pics the postID not work?

 

1. Is loading time a concern?

2. What kind of user load are you looking at?

3. Do you have enough space on mysql server?

 

You can always run a script to compress the pictures to specific size/type/quality before storing into the DB?

Link to comment
Share on other sites

Link to post
Share on other sites

Please do not store pictures in a database. While it is possible to serialze any data and stick it in there, it is a dumb thing to do.

 

The name of the picture should not matter and should not be dependant on the post it appears in or the user who uploaded it. Make use of the fact that you have a relational database at your disposal and design it in such a way that you relate posts and users to images by how the database is structured and not by how things are named. Here's a hint: you should have separate tables that keep track of posts, images, and the relationship of posts to images. That's three tables; the third is called a "join" or "junction" table.

Link to comment
Share on other sites

Link to post
Share on other sites

that's the thing, we only were provided with an mysql server, i've just sent a request for an ftp server. i'm thinking if we name the pic the postID it 'll all work.

There's an extension for myslq called like mysql blobs that allows you to store files.

i want to die

Link to comment
Share on other sites

Link to post
Share on other sites

maybe use an array ? as suggest store the name and extension (image.jpg) in the DB, add these to an array then eacho them in html

 

at the top of the page to populate the array

<?php 								$sql = "SELECT *image* FROM *table name*";		$result = $conn->query($sql);				if ($result->num_rows > 0) {						$image = array();						while($row = $result->fetch_assoc()) 			{                            array_push($a,".row["*image*"].");							}					} else 		{			echo "0 results";		}		$conn->close();?>

then to get the images

<img src="images/<?php echo $image[0]; ?>">

increasing number for each item.

 

I haven't tested this, just wrting this in my break at college it probably will have mistakes in there.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

I haven't tested this, just wrting this in my break at college it probably will have mistakes in there.

 

Yes, the mistake is using a relational database without actually having any useful relations. The OP wants and needs to associate each image with a post and user.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×