Jump to content

Display 'Blob' in HTML/PHP


Recommended Posts

I have loaded an JPEG image into mysqli database called hostajess and in table called images.  The binary is in my database but how to I write a php script to retrieve and display the image??

 

<?php
    error_reporting(E_ALL);
    if(isset($_GET['imgid']) && is_numeric($_GET['imgid'])) {
require_once('conn.php');
//connected to db

        $sql = "SELECT image FROM images WHERE imgid=2";

        $result = mysqli_query("$sql") or die("Invalid query: " . mysqli_error());

        header("Content-type: image/jpeg");
        echo mysql_result($result, 0);

        mysql_close($link);
    }
    else {
        echo 'Please use a real id number';
    }
?>

 

My database table is

imgid - int

image - blob

 

Thanks for any suggestions (by the way i realise using a file system might be easier, but i had less of an idea how to do that!!

 

Link to comment
Share on other sites

<?php
    error_reporting(E_ALL);
if(isset($_GET['imgid']) && is_numeric($_GET['imgid'])) {
require_once('conn.php');
//connected to db
$sql = "SELECT image FROM images WHERE imgid=2";

        $result = mysqli_query("$sql") or die("Invalid query: " . mysqli_error());

        header("Content-type: image/jpeg");
        $im =  imagecreatefromstring(mysql_result($result, 0));
        imagejpeg($im);
        imagedestroy($im);

mysql_close($link);
    }
    else {
        echo 'Please use a real id number';
    }
?>

Link to comment
Share on other sites

Start by putting both of these lines (you currently only have the second one) after your first opening <?php tag -

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

 

If you still get a blank page, that probably means you are getting a fatal parse error. You would need to turn on those two settings in php.ini instead to get php to tell you about any parse errors.

 

Next, you are mixing mysqli and mysql function calls (which cannot be done for a single connection or query) and the usage of the mysqli function calls is incorrect.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.