Display images from databse

Hello again,

My second noobie question in 30 mins.

I have this code…

<?php

//CONNECT TO THE DATABASE


$con = mysql_connect("xxxxx","xxxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("products", $con);

//GET DATA FROM DATABASE

$result = mysql_query("SELECT * FROM mainitems");

// HEADER
require("header.php");
require("home.php");



while($row = mysql_fetch_array($result))
  {
 
  echo $row['code'] . "<br />";
  echo $row['title'] . "<br />";
  echo $row['proddesc'] . "<br />";
  echo $row['img'] . "<br />";
  }


mysql_close($con);
?>

In the database in the field called “img” I store an image name such as image.jpg I want this image to display in the browser when I run this script. Can anyone tell me how to do this please?

The output I get at the minute looks like this…

0
test title one
test desc one
123.jpg
1
test title one
test desc one
logo.jpg
2
test title 2
test desc 2

3
test title 3
new desc

5
zxc
zxc
zxc
4
Niall item
Nlalls best item
niall.jpg

Many thanks that worked nicely. :slight_smile:

It could be that your images are placed in another folder.

If thats the case, then replace “path_to_images/” in this example, with the path to your images.


while($row = mysql_fetch_array($result))
  {
 
  echo $row['code'] . "<br />";
  echo $row['title'] . "<br />";
  echo $row['proddesc'] . "<br />";
  echo "<img src='path_to_images/".$row['img'] . "' /><br />";
  }

This does not work. Do I have to change anything in the code you gave me?



while($row = mysql_fetch_array($result))
  {
 
  echo $row['code'] . "<br />";
  echo $row['title'] . "<br />";
  echo $row['proddesc'] . "<br />";
  echo "<img src='".$row['img'] . "' /><br />";
  }

How might I enter this correctly into this code?



while($row = mysql_fetch_array($result))
  {
 
  echo $row['code'] . "<br />";
  echo $row['title'] . "<br />";
  echo $row['proddesc'] . "<br />";
  echo $row['img'] . "<br />";
  }


mysql_close($con);

Use the HTML img tag.