Jump to content

how to display image from mysql database to the web page

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
jadago

jadago

    Newbie

  • Members
  • Pip
  • 5 posts
I have tried to display the image from mysql but nothing happens to the web pages,the browser takes long time to output the result.i have used the following codes.
<?php
// connect to mysql server
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Not connected : ' . mysql_error());
}
// connect to database server
$db_selected = mysql_select_db('upload', $link);

if (!$db_selected) {
die ('Database error : ' . mysql_error());
}

$sql = "SELECT * FROM images";

// the result of the query
$result = mysql_query($sql) or die("Invalid query: " . mysql_error());

// Header for the image
header("Content-type: image/jpeg");
echo mysql_result($result, 0,'imageData');


?>
please help me

#2
fyhring4

fyhring4

    Newbie

  • Members
  • PipPip
  • 13 posts
Try something like this

<?php

$result = mysql_query($sql) or die("Invalid query: " . mysql_error());

$row = mysql_fetch_assoc($result);


$imgname = $row['img'];

function LoadJpeg($imgname)

{

    /* Attempt to open */

    $im = @imagecreatefromjpeg($imgname);


    /* See if it failed */

    if(!$im)

    {

        /* Create a black image */

        $im  = imagecreatetruecolor(150, 30);

        $bgc = imagecolorallocate($im, 255, 255, 255);

        $tc  = imagecolorallocate($im, 0, 0, 0);


        imagefilledrectangle($im, 0, 0, 150, 30, $bgc);


        /* Output an error message */

        imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);

    }


    return $im;

}


header('Content-Type: image/jpeg');


$img = LoadJpeg('bogus.image');


imagejpeg($img);

imagedestroy($img);

?>
Source: PHP: imagecreatefromjpeg - Manual

Edited by fyhring4, 18 December 2010 - 10:43 AM.