0

I'm new to php and am having trouble retrieving an image for display in a page. The image path is stored in a mysqli database under the column image. My code is listed below. Any help would be appreciated.

footwear.php

<img class="product-item_image" src="getImage.php?id=1" alt="Product">

getImage.php

<?php
$id = $_GET['id'];

$link = mysqli_connect("localhost","root","","dbname","3307");
$sql = "SELECT image FROM dbname.product_catalog WHERE id='$id'";
$result = mysqli_query($link,$sql);
$row = mysqli_fetch_assoc($result);

$image = $row['image'];
$image_src = $image;
mysqli_close($link);
echo "<img src='$image' >";
?>

when I navigate to getImage.php?id=1 the image is successfully displaying. However, when I'm viewing footwear.php the image is broken. I know there is something fundamentally wrong with my code, but I don't know what the next step is to get this to function as I'm intending.

Note, the Mysql database column holds the value "amour-03-bsat.jpg", but I'm unsure of how to get this value into footwear.php. I'm open to any other method of getting my image to display as well.

Thanks!

Mackeo
  • 11
  • 3
  • `getImage.php` should be spaffing out the [binary (base64)](https://stackoverflow.com/questions/1207190/embedding-base64-images) encoded image data *not* an HTML image tag if you want to take that approach. – CD001 Feb 15 '18 at 16:59
  • what does your rendered path display? – happymacarts Feb 15 '18 at 17:02
  • Looks like you'll be getting this `Product` on your page. Have you tried just echoing the `$image` variable? – TCooper Feb 15 '18 at 17:07
  • I tried echoing $image and it returns the correct path (amour-03-bsat.jpg) viewing getImage.php?id=1, which is great. However, the image still isn't displaying in footwear.php Do I need to echo this back to footwear.php somehow? – Mackeo Feb 15 '18 at 17:19

1 Answers1

0

You can do it like this

footwear.php

<?php
 require_once('getImage.php');
 $image = imagePath($id=1);
 <img class="product-item_image" src="$image" alt="Product">
?>

getImage.php

<?php
 function imagePath($id) {
  $link = mysqli_connect("localhost","root","","dbname","3307");
  $sql = "SELECT image FROM dbname.product_catalog WHERE id='$id'";
  $result = mysqli_query($link,$sql);
  $row = mysqli_fetch_assoc($result);

  $image = $row['image'];
  mysqli_close($link);
  return $image;
 }
?>

Hope it solves your problem.

Sathishkumar Rakkiyasamy
  • 3,509
  • 2
  • 30
  • 34
  • Hi Sathiskumar, Thank you for the example. footwear.php is a page which will have many thumbnails (a catalog). How would I modify this if there are multiple images I need to display on the page? Each item has its own unique id. For instance, in my example I have Product But there will also be Product a little further down on the page. – Mackeo Feb 15 '18 at 17:46
  • @Mackeo Is my answer helpful? – Sathishkumar Rakkiyasamy Feb 15 '18 at 17:48
  • Yes, I had to toy with it a little bit, but it ended up doing exactly what I needed it to do. Thanks! – Mackeo Feb 16 '18 at 01:10