954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Php mysql image link

I have a mysql table that has id, name, type, size, content, and link. I have pages download.php and showimage.php. I want the link to affect the image so when the user clicks it sends them their. I only created showimage.php cause I had ran out of options on how to link and was hoping i could extract the link somehow from download.php. Heres the code for both.

Download.php

<?php
include 'config3.php';
include 'opendb.php';

$query = "SELECT name, type, size, content, link FROM `upload` ORDER BY RAND() LIMIT 1";

$result = mysql_query($query) or die('Error, query failed'); 
while($row= mysql_fetch_array($result)){
$content = $row['content']; 
$size= $row['size']; 
$type= $row['type'];
 $link= $row['link'];
header('Content-type: $type');
echo $content;
}
include 'closedb.php'; 
?>


Showimage.php

<html>
<head>
<title>Image Test</title>
</head>
 
<body>
<h1>Displaying image from database</h1>
 
<a href="download.php"><img src="download.php" height="250" width="250"/></a> </body>
 
</html>


Download.php code above only shows the image. I've tried playing with download.php to echo "<a href='".$link."'/><img src='".$content."' /></a>"; but when i do this the image turns into a huge jarbled mess. Any suggestions?

RazorRamon
Junior Poster in Training
74 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 
fobos
Posting Whiz in Training
297 posts since Feb 2009
Reputation Points: 29
Solved Threads: 52
 

The tutorial you gave shows how to present the image.. which I'm already doing with the code above.. My problem is that the table also has a column for links. When the user clicks on the picture i want it to send the user to the appropriate address.

RazorRamon
Junior Poster in Training
74 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

Is the image stored as blob in your DB or is it a filepath?

Uploading image to blob: http://www.phpriot.com/articles/images-in-mysql/7
Extracting image and displaying it: http://www.phpriot.com/articles/images-in-mysql/8

(I don't like the use of mysql_result though)

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

I figured it out.. after banging my head on the keyboard for 12 hrs... lol. I'll post the code for the sake of all the broken computer equipment out there. Here's it is.

Showimage.php

<?php
include 'config3.php';
include 'opendb.php';

$query = "SELECT id, name, type, size, content, link " .
         "FROM  `upload` ORDER BY RAND() LIMIT 1";
		 $result = mysql_query($query) or die('Error, query failed');
while($row= mysql_fetch_array($result)){
	$content = $row['content'];
		$id= $row['id'];
	$size= $row['size'];
	$type= $row['type'];
	$link= $row['link'];
	}
?>
<html>
<head>
<title>Image Test</title>
</head>
 
<body>
<h1>Displaying image from database</h1>
<a href="<?php echo $link?>" />
<img src="download.php?id=<?php echo $id?>" height="250" width="250"/>
</a>
</body>
 
</html>


Download.php

<?php
include 'config3.php';
include 'opendb.php';

if(isset($_GET['id'])){
 $id=$_GET['id']; 
}else{
echo 'did not get id'; 
}

$query = "SELECT id, name, type, size, content, link " .
         "FROM `upload` WHERE id='$id'";

$result = mysql_query($query) or die('Error, query failed');
while($row= mysql_fetch_array($result)){
	$content = $row['content'];
		$id= $row['id'];
	$size= $row['size'];
	$type= $row['type'];
	$link= $row['link'];


header('Content-type: $type'); 
echo $content;
exit();
}


include 'closedb.php'; 


?>
RazorRamon
Junior Poster in Training
74 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: