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
https://forums.phpfreaks.com/topic/115341-display-blob-in-htmlphp/
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';
    }
?>

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.

Thanks for your suggestions! I have added that line of code and I also found i was missing quotes around my id number.

 

You said about the mysql and mysqli functions, which ones are incorrect? I have used mysqli in my other queries which are working well if that matters??

 

Thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.