James Parsons
James Parsons

Reputation: 925

Displaying a date specific image from a databse

The aim is for a customer to upload their advert to the database, that triggers and email for approval. Once approved ad will display in location between the set dates.

I have written the code correctly to display by date and approval from an admin area. I just cannot get the image to display.

The code below details the process so far.

    <?php 
include('mysql_connect.php');{

  $location='1'; 
  }

 $resultSet = $mysqli->query("SELECT * FROM adverts WHERE adloc = '$location' AND approval ='Y' ");

if($resultSet->num_rows > 0){
    while($rows = $resultSet->fetch_assoc())
    {
        $id = $rows ['id'];
        $start = $rows ['start'];
        $end = $rows ['enddate'];
        $business = $rows ['business'];
        $email = $rows ['email'];
        $tel = $rows ['tel'];
        $web = $rows ['web'];
        $advert = $rows ['image'];

    $Date = date('Y-m-d');
    $Date=date('Y-m-d', strtotime($Date));; 
    $DateBegin = date('Y-m-d', strtotime("$start"));
    $DateEnd = date('Y-m-d', strtotime("$end"));
}

if (($Date > $DateBegin) && ($Date < $DateEnd))
    {
      echo "ADVERT";
      echo '<img src="getad.php?id=$id">';

    }
    else
    {
      echo "FILLER IMAGE";  
    }

}
?>

The code for the getad.php file is as follows

if(isset($_GET['id']))
{

$id = mysqli_real_escape_string($_GET['id']);
$query = mysqli_query("SELECT * FROM adverts WHERE id= '$id'");
while ($row = mysqli_fetch_assoc($query))
{
    $image = $row['image'];
}
header("content-type: image/jpeg");
echo $image;
}
else
{
    echo "Error!";
}
?>

I am sure I can't be far off. The image is a JPEG LONGBLOB in the database.

Thank you.

Upvotes: 0

Views: 54

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74220

Firstly, this is failing you:

$id = mysqli_real_escape_string($_GET['id']);
$query = mysqli_query("SELECT * FROM adverts WHERE id= '$id'");

Both of those functions require a database connection be passed and as the first parameter:

$id = mysqli_real_escape_string($mysqli, $_GET['id']);
$query = mysqli_query($mysqli, "SELECT * FROM adverts WHERE id= '$id'");

References:

However, may need to use base64_decode:

header("content-type: image/jpeg");
echo base64_decode($image);

or

echo '<img src="data:image/gif;base64,' . $image . '" />';

Here is an example borrowed from https://stackoverflow.com/a/20564797/

$db = mysqli_connect("localhost","root","","DbName"); //keep your db name
$sql = "SELECT * FROM products WHERE id = $id";
$sth = $db->query($sql);
$result=mysqli_fetch_array($sth);
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'"/>';

For debugging purposes:

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.

  • As well as or die(mysqli_error($mysqli)) to mysqli_query().

Upvotes: 1

Related Questions