942,528 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 828
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 25th, 2010
0

Calling images from MySQL using PHP

Expand Post »
Hi there, does anyone know any good tuts for displaying images using php. The thing is, I have a site that i search for a part number and it displays the info, that i can do but for some reason im lost when it comes to the images.

Do i save them in a db

Do i save them as link and save the link in a db

I just dont really know where to start, im self taught php and so a little help would be much appreciated

thanks
Dan
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
dandixon is offline Offline
62 posts
since Oct 2009
Jan 25th, 2010
0
Re: Calling images from MySQL using PHP
You can save images as data within a blob field. But to be honest, I'd stick with an image folder with a reference to the url in the DB.

images table
id (PK)
displayname (could be a false filename or title)
extension (file type, e.g. jpg/png/gif/svg)
tags (useful for searching)
description (useful for searching)
uploader_id (user id)
uploaded_timestamp (unix integer timestamp)
(etc)

This is how I usually set my db:

In order to stop duplicate files and overwriting:

1) Send form (with upload file and info on picture)
2) Add new entry to DB, filling in details from $_POST.
3) Get PK value from mysql_insert_id() function and rename the image file to "img_" & id value followed by the original extension.
4) All images now are named: img_1.gif, img_2.jpg, img_3.jpg, img_4.png etc.

Overwriting is no longer an issue. Replacing an image is more straightforward that it would suggest.

Anyway, you can do all this without using the 'id' method - just copy the proper filename to a filename field.
Sponsor
Featured Poster
Reputation Points: 1034
Solved Threads: 929
Sarcastic Poster
ardav is offline Offline
6,560 posts
since Oct 2006
Jan 26th, 2010
0
Re: Calling images from MySQL using PHP
i hear where you are coming from and a understand most of what you say it's just im not fluent in php and am not sure how to call files from a folder
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
dandixon is offline Offline
62 posts
since Oct 2009
Jan 26th, 2010
0
Re: Calling images from MySQL using PHP
OK mate, I hear you, but I don't want to be acting the sap and do the work for you. Clue:

PHP Syntax (Toggle Plain Text)
  1. <img src="<?php echo $img;?>" ...(other attributes perhaps) />

Where the $img variable has come from the db. It's just a path and a filename.
Sponsor
Featured Poster
Reputation Points: 1034
Solved Threads: 929
Sarcastic Poster
ardav is offline Offline
6,560 posts
since Oct 2006
Jan 27th, 2010
0
Re: Calling images from MySQL using PHP
Hi Dan,

I'm willing to give this a try. I am also a PHP MySQL novice. I'm not sure how much you understood from the preceding post so I'm going to start at the beginning.

You don't put pictures in a database. Databases are records of text and numbers. You'll need to upload your pictures into a folder or directory.

Let's say that your URL is:

http://www.yoursite.com/

A folder or sub directory that you put your photos in could be called:

http://www.yoursite.com/images/

You upload all of your photos into that directory.

Now the simplest way you could have a database for your records is:

An Item number and a photo address that corresponds to that item number

Let's assume that your photos are in .jpg format.

Here's how the MySQL database will look:

Record 1
------------

Item Number: 1

Photo Address: /images/img_1.jpg



Record 2
-------------

Item Number: 2

Photo Address: /images/img_2.jpg

And so on...


Of course you may have more information for the record than just item number and photo address. You might also have the manufacturer or other information.

I hope this helps.

Larry
Last edited by Larry_b; Jan 27th, 2010 at 3:29 am. Reason: Mispelled a word
Reputation Points: 12
Solved Threads: 4
Newbie Poster
Larry_b is offline Offline
16 posts
since Jan 2010
Jan 27th, 2010
0
Re: Calling images from MySQL using PHP
Assign $row['pic'] to a variable. Then put it in an image tag.

$pic = $row['pic'];
echo "<img src = '$pic' >";

Also, just putting the name of the file, instead of the full path, in the database might work better. In which case, you would put the relative path in the img tag before the pic variable. Like this.

echo "<img src = 'images/$pic' >";
Reputation Points: 10
Solved Threads: 0
Newbie Poster
KimAnderson is offline Offline
2 posts
since Jan 2010
Jan 27th, 2010
0

im getting it now

Thnks guys for your help, by any means i dont want it doing for me becuase i really want to learn this, just the process in my head and the starting point like you have given me.

Really appreciate it, im going to go and right some code and ill let you know where i get to and if i hit any snags

thanks
dan
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
dandixon is offline Offline
62 posts
since Oct 2009
Jan 27th, 2010
1

in over my head

thanks to you guys i've figured out the image problem i had and now opened a whole new can of worms. I think i overcomplicated it in my head but once i sat down and thrashed out the code it wasnt too bad.

the new problem which i may hav to make a new post for is i need to query the db and return a part number and model, but the thing is these results need to link a page where they are displayed fully

heres an example http://www.airsprings.com/search/airsprings

if u type in the bottom box W02-358 in returns 3 results which are in turn linked to a page where they display fully.

Im not asking for the code or anything doing for me but suggestions of the best way to go about this

thanks again for all your help
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
dandixon is offline Offline
62 posts
since Oct 2009
Jan 27th, 2010
0
Re: Calling images from MySQL using PHP
Hi Dan,

I am new here and I'd definitely don't want to break any rules. There seems to be a rule about giving too much help, but I'm finding with your post that it feels like I'm trying to read your mind. I'm also having a little trouble with your terminology. Your use of the word link seems to be used for different things. I'm not criticizing you, it's just that I'm having some problem with that. You may use that terminology in Great Britain. I'm from the States.

I also hope that my previous example wasn't too simplistic. I didn't mean to insult your intelligence by starting at step one. If I was then please accept my apologies.

Now to the problem. So you are querying the database and that database query results in a part number and model name. Is this going to be a unique number and model? In a way it doesn't matter. I'm just trying to understand the problem.

When you say "link to a page where they are displayed fully", do you mean to display (load) a new page where you can display the contents/result of your database query? For now I'm going to assume that is what you're asking.

You may want to look at the PHP header() function. Specifically, header('Location: http://www.yoursite.com/'); that will load a new page. http://www.yoursite.com/ can be a local file name such as newfile.php. Also since it's a PHP command you can send arguments to that page.

If this isn't what you're looking for, and you haven't figured it out yet, then please rephrase the question and I'll try to do what I can.

I'm wondering if this isn't the more difficult way to do things. Again, I don't want to insult your intelligence, but I was wondering. If you take the input from the form and the action="newfile.php" in the HTML form. Wouldn't that display a new page where you can then call the database and display the results? I'm just thinking out loud here. Unless of course you are not using a form to get the information to query the database for the part number and model name. In that case the PHP header() function might be what you're looking for.

Good luck,

Larry

PS - More info on PHP header() funciton can be found at:

http://php.net/manual/en/function.header.php

and

http://www.w3schools.com/php/func_http_header.asp
Reputation Points: 12
Solved Threads: 4
Newbie Poster
Larry_b is offline Offline
16 posts
since Jan 2010
Jan 28th, 2010
0
Re: Calling images from MySQL using PHP
Hi Larry

I have had this problem before with terminology, by any means i dont want to break rules and i dont want any code writing for me becuase i want to to learn if not whats the point, i may as well just employ someone.

I just want to know some ideas about the best way to go about doing things and make sure i do them in the right order.

The best way for me to explain things is to go to the link that i posted http://www.airsprings.com/search/airsprings and type this "W02-358" without qoutes into the botto box, as you will see this brings multiple results, which is the point where i have got to in my own script, but then if you click one of the results it taks you to a page where it displays the entire details for that result.

I just need a few suggestions about how i would do the same

Thanks again for helping me and im sorry about the accent
Dan
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
dandixon is offline Offline
62 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: How to pass a PHP Variable to an AJAX function as a parameter
Next Thread in PHP Forum Timeline: Php help plzz





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC