Jump to content
xisto Community
Sign in to follow this  
midnightvamp

Can You Add Images Into A Mysql Database? Using Php?

Recommended Posts

php displaying an image out of mysql

Can You Add Images Into A Mysql Database?

 

HI I done a Google search and found this forum and I have been having trouble displaying images in a html form.

 

Ok I have a working upload script I made which I tested it and it works it stores the username and the link to where the images is . I know how to display one image but I have a max limit to the user of 12 pictures and I don't fully know how I can display all 12 for the user.

 

I have the user to have his own page where only the images he uploaded he can look at.

 

I so far made the script making an array and using a while loop but didn't succeed on doing this.

 

So right now I have the username and the link of the image in the database and now trying to display those images in a html table.

 

 

Thanks.

 

-reply by aaron

Share this post


Link to post
Share on other sites

OK, there is in fact way to store images however I do not know who would like to use it since it is not that simple to use. Currently the simplest way has been already provided here and it is saving links in the mysql database.However there is also another thing person could do since there are some readers of files and so on integrated in the php you can find one adn use it to read the binary source code of the image and then effectively save binaries in the database however then again when you want to provide your users images then you would have to generate image back. Though this can be really useful if you are making some security integration in your script this could provide you with the ability to generate new images from randomly generated strings. Or somewhere else I cannot remember now the thing is that this can be ussuful however the problem is it is a bit complicated. O yes I have remembered one more use case. When let's say you do not have rights to write files on the server however you have rights to write in the database you would use database and it woudl solve your problems :lol:

Share this post


Link to post
Share on other sites

view image from database at single page

Can You Add Images Into A Mysql Database?

 

How to view all stored image from mysql database

I wan to store and display

 

Title:

Comment:

Description:

File:

 

These are the fields I have.

 

-question by satheesh

Share this post


Link to post
Share on other sites

Storing an image as an image

Can You Add Images Into A Mysql Database?

 

Hello,

 

I have a problem with storing images in my database so I started searching the web. It seems like everybody is with storing the link of the image rather than storing the image itself in the database.

 

I don't know if My case is different, I am designing a website with which people fill in a form and submit their images (as part of the application). Isn't it better to store the image itself rather than the url since images will be at a remote location?

 

I have used longBLOB and it seems to store the image as a string of numbers but I am not sure how to retrieve the image as an image not numbers.

 

How do I go about it? Can anybody help?

 

Thank you

 

-reply by LittleB

Share this post


Link to post
Share on other sites

Please give me the code for loading images into mysql db 5.It works with easy php 5.6. The web server is apache 2.2. I'm new for php and mysql. Please help me with detailed steps. Thank you-reply by mini

Share this post


Link to post
Share on other sites

To display binary data stored in a database you have to be able to generate formatted header information. You would be getting into something like the following:

 

<?phpGlobal $blobId;$blobId = "1"; //set the blobId for debugging purposesIf(!is_numeric($blobId))Die("Invalid blobId specified");Require_once ('connecttodatabase.Php'); //connects to the database$dbQuery = "SELECT blobType, blobData ";$dbQuery .= "FROM myBlobs ";$dbQuery .= "WHERE blobId = $blobId";$result = mysql_query($dbQuery) or die("Couldn't get file list");If(mysql_num_rows($result) == 1){$fileType = @mysql_result($result, 0, "blobType");$fileContent = @mysql_result($result, 0, "blobData");Header("Content-type: $fileType"); //type is correct as image/jpegEcho $fileContent; // displays garbage instead of image}Else{Echo "Record doesn't exist.";}?>

-reply by bystander

 

Share this post


Link to post
Share on other sites

I tried to convert my page to php, by I messed it up big time, and couldn't figure it out, so I'm trying again.

What server-side language are you using currently? You might want to just go with what you have instead of porting it to PHP, unless you have a specific reason to switch over.

Should I be getting the page to work in php first, and then try to add the database? Or do it all at once? What do you find works best?

If your code doesn't compile, it's pretty much useless so you might as well just go with the barebones and build up from there.

In class, the teacher just gives us a file, and basically tells us to change the variable names, so I haven't learnt much other than what I've taught myself either, but I'm way behind you lol... and hey, bad habits, aren't really bad habits so long as they get the job done.

The teacher teaches you to change variable names?!?! I'd seriously consider moving to a different institution if that were the case.

One thing my teacher does say is that it doesn't matter what means you take to get the job done with a client. So long as it works in the end, the client isn't going to know/care depending on how much they know about programming.

It depends. If you have a client who knows nothing about computers and how the software is supposed to work, you can get away with selling junk. On the other hand, a client with more experience in dealing with such systems would say, "Hey, this isn't running efficiently - I won't accept it till it's been optimized and runs fast enough. See the code in file XYZ.php to see what I mean" Then, there are others who have a serious lack of understanding of how computers work but they say, "Why is it taking so long? Go to Google.com and see how quickly it loads." (although you've been making a couple of database transactions and generating images on-the-fly, while Google simply displays a form for search)

Share this post


Link to post
Share on other sites
Storing an image as an imageCan You Add Images Into A Mysql Database?

You have stored your image in your mysql database, now you want to retrieve it.

create a new page, call it image.Php

Add your database connection scripts, I will not be doing that here, if you don't know that step you shouldnt be at this step.

<?php

// Add your database connection code here// This will be the id field, your primary key for your image table. If you do not have a primary key, go back and make one. All tables should have one for easy indexing. Use mysql_real_escape_string on all URI variables to prevent SQL injection attacks.$id = mysql_real_escape_string($_GET['id']);

$result = mysql_query("SELECT * FROM images WHERE id={$id}");If(mysql_num_rows($result) == 1){ $image = mysql_fetch_assoc($result); $imageType = $image['image_type']; // You should have stored the type when you stored the image $img = $image['image'] // This is the field where the longblob of the image is stored. header("Content-type: {$imageType}"); print $img;}

?>

Now, in your HTML use:

<img src="image.Php?id=1" alt="" />

Now you go to the file itself http://forums.xisto.com/no_longer_exists/

-reply by Adam

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.