php, mysql and images ... ARGH!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

nVee
Forum Newbie
Posts: 11
Joined: Mon Mar 31, 2008 9:44 am

Re: php, mysql and images ... ARGH!

Post by nVee »

thank you for ur help.

How will i now actually get the link to be stored in the database, and then later when i recall it, how will the server know its a link *how will the actual recall of the image be made*.

Thank you so much for your help so far!
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: php, mysql and images ... ARGH!

Post by aceconcepts »

In order to insert the image into the database all you need to do is specify the name of the image file:

Code: Select all

 
//GET THE IMAGE NAME FROM THE UPLOAD
$imgName=$_FILES['img']['name'];
 
//INSERT THE DATA - NOTE THE NEW VARIABLE $imgName
$sql = "INSERT INTO bushshirts (img,name,description) VALUES ('$imgName','$name','$description')";
 
Now, in order to retrieve the image file and make it a link you would do:

Code: Select all

 
//SPECIFY WHERE THE IMAGES ARE STORED e.g. "http://www.site.com/images/uploads/"
$directory_to_uploads
 
//GET THE CORRECT RECORDS/PRODUCTS
$listProducts=mysql_query("SELECT * FROM bushshirts ORDER BY name");
 
//CHECK IF ANY PRODUCTS EXIST
if(mysql_num_rows($listProducts)>0)
{
//NOW OUTPUT THE RESULTS
while($productRow=mysql_fetch_array($listProducts))
{
echo $productRow['name'] . "<br />";
echo $productRow['description'] . "<br />";
echo '<a href="#"><img src=" ' . $directory_to_uploads . $productRow['img'] . ' " /></a>';
}
}
 
It's pretty darn crude but it's one way of storing and retrieving an image.
nVee
Forum Newbie
Posts: 11
Joined: Mon Mar 31, 2008 9:44 am

Re: php, mysql and images ... ARGH!

Post by nVee »

Well with the help of a friend, we managed to rather take the alternative route and put the images directly into the database. Although this is a longer route, we save the effort of likely over-writing some filenames and it loads fast enough ...

Our trouble came in to view the image later, only too find out you have to use a "image replacement file", or a php file with a script to actually view the image, with my little php knowledge, it was a ideal scenario where i had to use the little i know about the php and some common sense and BAM, there the images show and i learnt a great load with this exercise!

Thank you soo much for your help, i think i am falling in love with php, its definately easier than playing the guitar, but i also know i still have ALOT to learn, i just hate reading so darn! But knowledge is power, or atleast money in the bank :)

speak 2 u soon :)
altoyes
Forum Newbie
Posts: 2
Joined: Thu Apr 03, 2008 8:54 pm
Location: australia brisbane

Re: php, mysql and images ... ARGH!

Post by altoyes »

aceconcepts wrote:So you need to upload the image to a directory and also store it in the database and then retrieve it also?

Correct?
hi aceconcepts

do you happen to have a complete script which is working?
as i cannot use the snippets of code you have posted thus far,
as it applies to nVee

thankyou for your time.

alto
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: php, mysql and images ... ARGH!

Post by aceconcepts »

What exactly do you need?
Post Reply