Jump to content

help with displaying images


vmicchia

Recommended Posts

So I am trying to display an image from a database (I know it's not the best idea but it's what I was told to do). Anyway I'm having problems displaying it. Here is the code for displaying the image.

$cat = $_GET['cat'];
include 'includes/openDbConn.php';
$sql = 'SELECT * FROM CushionsCategories WHERE CushionCategory = "'.$cat.'"';
echo $sql;
$result = mysql_query($sql); 
echo '<table>';
while($val = mysql_fetch_assoc($result)){
	$cush = 'SELECT * FROM Cushion WHERE SKU = "'.$val['CushionSKU'].'"';
	echo $cush;
	$cushres = mysql_query($cush);
	$cushval = mysql_fetch_assoc;
	$img = 'SELECT * FROM Images WHERE SKU = "'.$val['CushionSKU'].'"';
	echo $img;
	$imgres = mysql_query($img);
	$imgval = mysql_fetch_assoc($imgres);
	if( $i % 3 == 0 ) {
	      echo '</tr><tr>';
  			}
	echo '<td><img src="image.php?sku='.$val['CushionSKU'].'" name="'.$imgval['FileName'].'" description="'.$imgval['Description'].'" /></td>';
}
echo '</tr></table>';

 

and the page that gets the image:

$sql = 'SELECT Image FROM Images WHERE SKU = "'.$sku.'"';
//echo $sql;
$result = mysql_query($sql) or die("Invalid query: " . mysql_error());

        // set the header for the image
        header("Content-type: image/jpeg");
        echo mysql_result($result, 0);

 

Thanks for any help in advance.

 

 

Link to comment
Share on other sites

Just put the image name in the database ie beach.jpg under a table called image run the query and do the image source as

echo "<img src='$image'>";			

 

already stated that image is stored in the database and needs to pull image from database and send directly to browser, thus no possibility for a simple image link to a file.

Link to comment
Share on other sites

I had looked at this link before, it seemed a lot like the tutorial I was using. Anyway I tried it the way this shows. I changed the code to how this tutorial shows and it is still not working for me. When I go to the image.php it echos out Array like I assume it should but on the page the image is displayed I still get a broken link. here is what the code looks like now:

$sql = 'SELECT Image FROM Images WHERE SKU = "'.$sku.'"';
//echo $sql;

$result = mysql_query($sql); 
if (mysql_num_rows($result) != 0) { 
  $row = @mysql_fetch_array($result); 
  $image_type = $row["Type"]; 
  $image = $row["Image"]; 
  header ("Content-type: $image_type"); 
  print $image; 
}

 

Edit: Also when I do a print_r on the results it still only says array rather than giving me the contents of the array.

 

Link to comment
Share on other sites

Here's the actual code. I got it figured out though. I was just sending a variable wrong.

 

$sku = $_GET['sku'];
//echo $sku;
include 'includes/openDbConn.php';
$sql = 'SELECT * FROM Images WHERE SKU = "'.$sku.'"';
//echo $sql;

$result = mysql_query($sql); 
if (mysql_num_rows($result) != 0) { 
  $row = mysql_fetch_assoc($result); 
  $image_type = $row["Type"]; 
  //echo $image_type;
  $image = $row["Image"]; 
// echo $image;
  header ("Content-type: $image_type"); 
  echo $image; 
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.