Jump to content
xisto Community
Sign in to follow this  
midnightvamp

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

Recommended Posts

I'm learning php in class right now, but I'm still not that good at it, what I'm wondering is when I write the php so that it can connect with a database, can I at the same time have it that it is able to display back images that I choose.Like, I want a search feature, where you can search for a keyword, and it will bring back a list of all the possible entries with that keyword, but each of these entries will have a photo associated with it.Now, do I put these image files directly into the database, or do I write the code to link them from my files to the other information that will be stored in the database? I've looked in Google, and around this forum, but can't really find what I'm looking for.Any suggestions?

Share this post


Link to post
Share on other sites

I have done something like this with my site http://www.mudmall.com/ The database contains lnks to each of the images. I suggest you use thumbnails and limit the number of images shown on each page. I wrote the code myself and because I have taught myself it may not follow the correct conventions, therefore I won't show you the code unless you ask for it. I don't want to teach you any bad habits. :blink:

Share this post


Link to post
Share on other sites

I roamed around at the my_sql site and could find no reference to storing actual images in the Database, but like Avalon said, the best you will probably come up with is to use the DB to store the Image name, maybe a reference Number and other info about the image. I don't think Databases store the right amount or type of data to be able to store an actual image format.

Share this post


Link to post
Share on other sites

That's pretty much what I'm after, Avalon, just with a couple different things like I want to have the date as a field as well.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. 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?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.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.

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. 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?

Get the page working in PHP first and then when that's right, you can replace actual values with those that come from the database.

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.

That's not strictly true. To say it doesn't matter what means you use is, quite frankly, irresponsible.
For instance, you could set up a contact form on a site and through lax programming, leave it open to email header injection attacks so that your client's mail server gets used to transfer loads of spam.
The client may never find about it but could gain a bad reputation as a spammer because the mail all appears to be coming from his/her email address. Or the client's web host may suspend or terminate the account due to the abuse.
So, if you are doing paid work for a client, it is in your best interests to know as much as you can about what you're doing if you want to avoid having to pay to fix ealier errors, or worse still, be sued due to lost business caused by your oversights.

Share this post


Link to post
Share on other sites

hm there is also another possibilty to store image files in a data base apart from saving the link and some other info. what you can do is store the image in the database as a binary data. then you can have like lets say id be the primary key and have it auto increase and then you can call the image up with a small script to call the picture and then have it be like

<img src="img.php?id=2">
for the second image in your database. i can show you the code if you want and its not a very big one either. it has an upload page and like an image.php page. you can password protect your upload page to prevent people messing around with it.

and as for the search thing you can just add another column to your database table called Name or Keywords and then have a list of words in there and have a search thing where you have the mysql query WHERE $keyword or something like that. well not like that but thats how you would build up the code for searching.

tell me if you want to try this and ill give you the code or if you dont understand something or yeah.

Share this post


Link to post
Share on other sites

Yes you could store the images in the database as binary data, that's true. However, I think there are some major drawbacks in doing that, mainly with the size the database will become with each record being the equivalent size of the image itself. Correct me if I'm wrong, but I think that is what will happen. I also wonder what sort of load it will put on the server and how it will affect the speed of the database once more than a few records are added.

 

Tyssen is totally correct about learning to program correctly. If someone is teaching you, they should be teaching you the right way to do things, not just whatever will work. I also agree with Tyssen about the way to start. Decide on your layout, what fields you want to display and possibly use for calculations and then build your database and use those fields in your pages. The fields you see on my pages are not all the fields in the database. In the images table I also keep track of how many times an image is viewed at full size and the date it was last viewed. I have other tables for the banner accounts, searches and page view statistics. The fields and tables you use are totally up to you and what is needed to get the results you want.

 

The database has evolved a little, originally I used to keep tracked of the image and thumbnail names, with careful naming of the thumbnails, I only need the image names. I also used to enter the image and file sizes of the images in the database, now I use php functions to calculate those on the fly. I could also use php to generate thumbnails on the fly, but I have chosen not to so I have a little more control over what is seen by visitors.

 

Start simple and build from there, like any site, you will find it will evolve over time as you learn more. To start you with a few ideas, here is a link to the code (in a text file) for the page that lists the thumbnails based on search criteria. http://www.mudmall.com/other/list.txt As I said the code is most like not totally correct and there are probably more efficient ways of doing what I have done, but it does work. :blink: (To those out there that know their php, I greatfully accept any suggestions given to improve my code, I am sure it can be improved upon)

Share this post


Link to post
Share on other sites

Why don't you just insert the links to the images in your database? You can have a folder in your site where all the images are uploaded and you only need to store the location of the image in the database. If it's in the folder pictures you enter something like this in your database. "pictures/image1.jpg"

 

That way the actual data (flat string or numeric) is separate from your images. If you include the picture in the database, you will have problems with editing the pictures since it's not the usual text. You might also encounter format issues when querying from your database since you have to do some conversion to insert the data and to query or select data. :blink:

Share this post


Link to post
Share on other sites

For instance, you could set up a contact form on a site and through lax programming, leave it open to email header injection attacks so that your client's mail server gets used to transfer loads of spam.

The client may never find about it but could gain a bad reputation as a spammer because the mail all appears to be coming from his/her email address. Or the client's web host may suspend or terminate the account due to the abuse.

So, if you are doing paid work for a client, it is in your best interests to know as much as you can about what you're doing if you want to avoid having to pay to fix ealier errors, or worse still, be sued due to lost business caused by your oversights.

220848[/snapback]

Sorry to hijack this thread a little, but how do you protect against this problem? Through ignorance, I may have inadvertantly left myself open to this type of attack.

Share this post


Link to post
Share on other sites

Sorry to hijack this thread a little, but how do you protect against this problem? Through ignorance, I may have inadvertantly left myself open to this type of attack.

I'm by no means a programming expert, so in cases like this I resort to using others' scripts that have been tried and tested and proven to be secure.

Share this post


Link to post
Share on other sites

you can store images using the blob data type but as mentioned before, don't store the image in the database because it takes up too much space. just store it in a folder and store the link in the database.

Share this post


Link to post
Share on other sites

Hi, I have a problem. I'm uploading an image to a database but I want it to send error message that I have already uploaded a file for the specific record. What should I do?-kamiya_ayanami

Share this post


Link to post
Share on other sites

Hi can you show me the code of inserting pics into a MySQL database.

Can You Add Images Into A Mysql Database?

 

Replying to Avalon

 

Much better if the procedure is included. Am a novice programmer and very much willing to learn more about web developopment.

Hope you can help me.

 

Thanks,

Tin

Share this post


Link to post
Share on other sites

inserting/retrieving pics in MySQL dbase/ PHP

Can You Add Images Into A Mysql Database?

 

Replying to sxyloverboy

Hi there, I tried inserting pic in MySQL using BLOB as the data type.

But it only converts the link to binary characters.

When I retrieved it, it only display the binary character to my web browser.

I observed that the data type (BLOB) is in binary.

I was thinking of changing it into something else but I don't know how.

Hope you could help me.

 

Thanks.

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.