1

I have a sqlite db and like to load an image out of it and show it on site. Unfortunately, once I echo the image (which works) nothing else will be shown. So I guess I am missing something here.

That's my code:

get_image.php

<?php
  function get_icon($id){
    $db = new PDO('sqlite:rdb.db3');
    $sql = "SELECT * FROM icons WHERE _id = " . $id;
    $query = $db->query($sql);
    $result = $query->fetch(PDO::FETCH_ASSOC);
    header("Content-Type: image/png");
    echo $result["bytes"];
  } 
?>

show.php

<?php
  ini_set('display_errors',"1");
  require_once 'get_image.php';
  $id = $_GET['id'];
  $icon = get_icon($id);
?>
<p>Show further content...</p>

Anyone who could possibly help me out here?

Thanks in advance! :)

Andreas

1
  • Use the browser developer tools to inspect the pages and look for errors. If you comment out the call to get_icon() does anything change? What steps have you taken to isolate the problem? Commented Jan 24, 2018 at 15:47

2 Answers 2

1

Your php script is echoing out an image but you probably want to echo out html that includes the image.

To do that, you can set the script as the source of an image in your html:

<img src="get_image.php?id=XXX">

Note that you would have to move the code that handles the id from show.php to the get_image.php script.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to create an <img/>-tag which refers to a URL/path that then returns the image. Something like

<img src="/get_image.php?id=xxx"/>

1 Comment

@AndreasB if this solves your problem, please mark the answer as solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.