0

Im trying to resize and display an image from my MySQL database. Im able to display the image but when I try to put it in a table or size it, it shows no image. I attached a screen shot of what I get when I try to add any style to it.screenshot If I display it with no style it works fine. Below is the my code.

<?php
if(!empty($_GET['id'])){
//DB details
$dbHost     = 'localhost';
$dbUsername = 'user';
$dbPassword = 'pswd';
$dbName     = 'db';

//Create connection and select DB
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);

//Check connection
if($db->connect_error){
   die("Connection failed: " . $db->connect_error);
}

//Get image data from database
$result = $db->query("SELECT receipt FROM Expense WHERE id = {$_GET['id']}");

if($result->num_rows > 0){
    $imgData = $result->fetch_assoc();

    //Render image
    header("Content-type: image/jpeg");

      echo "<tr>
      <td>".$imgData['receipt']."</td>
      </tr>";

}else{
    echo 'Image not found...';
} 
}
?>
Rene Robles
  • 83
  • 1
  • 6
  • 3
    This looks like pretty old code. You should not be using `mysql_` in PHP, nor `table`s for design in HTML. Use CSS. Additionally your SQL is open to injections. – user3783243 Jan 10 '20 at 19:07
  • What do you mean injections? – Rene Robles Jan 10 '20 at 19:28
  • 1
    See https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php Basically `$_GET['id']` comes from user input and could contain anything, additional SQL functions can be added and users/bots can manipulate your data... or obtain data they shouldnt have access to – user3783243 Jan 10 '20 at 20:01
  • What is `receipt`, a blob or a file location on your server? – user3783243 Jan 10 '20 at 20:04
  • That is temporary for testing – Rene Robles Jan 10 '20 at 20:52
  • Yes on receipt. Just a picture image of a receipt – Rene Robles Jan 10 '20 at 20:52
  • What is in the database a blob or string that contains the files location? – user3783243 Jan 10 '20 at 20:54
  • Images are stored in the database as BLOB. More specifically LONGBLOB – Rene Robles Jan 10 '20 at 22:48
  • 1
    Okay, so either base64 encode the return in an `src` of an `img` tag, or make a page that will render the image. You can't use `image/jpeg` with `HTML`. All the bytes would need to compile an image – user3783243 Jan 10 '20 at 22:50
  • It's unusual to store images this way , especially if larger than 100kb, say - as I suspect these are. – Strawberry Jan 10 '20 at 23:53
  • Yes they are. I’m using MySQL database for expense recording with receipt images. What’s another way to store the receipts images but linked to the expense data in the database? I have hosting space with hostgator. – Rene Robles Jan 11 '20 at 04:51
  • I rewrote the code. I'm getting I guess binary gibberish on my screen. '; } mysql_close($connect); } ?> – Rene Robles Jan 13 '20 at 05:43

0 Answers0