user970225
user970225

Reputation: 145

cannot display image in PHP

here's my code for image display -

$username = "xxxxxxxx";
$password = "xxxxxxxx";
$host = "000.001.000.000";
$database = "xxxxxxxx";

@mysql_connect($host, $username, $password) or die("Can not connect to database:         ".mysql_error());
@mysql_select_db($database) or die("Can not select the database: ".mysql_error());
$query = mysql_query("SELECT mimetype, Image FROM table ORDER BY id DESC LIMIT 0,1");
$row = mysql_fetch_array($query);
$content = $row['Image'];
header('Content-type: image/jpg');
echo $content;

This is the error i'm getting

The image “http://www....” cannot be displayed because it contains errors.

what is wrong? The datatype of field in mysql is mediumblob

Upvotes: 0

Views: 1705

Answers (3)

Luke Stevenson
Luke Stevenson

Reputation: 10351

OK, first test, to see what is happening:

$username = "xxxxxxxx";
$password = "xxxxxxxx";
$host = "000.001.000.000";
$database = "xxxxxxxx";

if( !mysql_connect($host, $username, $password) )
  die( 'Unable to connect to Server: '.mysql_error() );
if( !mysql_select_db($database) )
  die( 'Can not select the Database: '.mysql_error() );

$query = mysql_query( "SELECT mimetype, Image FROM table ORDER BY id DESC LIMIT 0,1" );

if( !$query )
  die( 'Query Failed: '.mysql_error() );
if( mysql_num_rows( $query )==0 )
  die( 'Query Returned No Records' );

$row = mysql_fetch_array($query);

echo '<pre>';
var_dump( $row );
echo '</pre>';

That should either show you the results from the database, or an error message. If you see an error message, correct whatever is causing it...

After the above just returns the Database row contents:

$username = "xxxxxxxx";
$password = "xxxxxxxx";
$host = "000.001.000.000";
$database = "xxxxxxxx";

if( !mysql_connect($host, $username, $password) )
  die( 'Unable to connect to Server: '.mysql_error() );
if( !mysql_select_db($database) )
  die( 'Can not select the Database: '.mysql_error() );

$query = mysql_query( "SELECT mimetype, Image FROM table ORDER BY id DESC LIMIT 0,1" );

if( !$query )
  die( 'Query Failed: '.mysql_error() );
if( mysql_num_rows( $query )==0 )
  die( 'Query Returned No Records' );

$row = mysql_fetch_array($query);

header( 'Content-type: '.$row['mimetype'] );
echo $row['Image'];

(assuming that the mimetype field is something like "image/jpg")

Upvotes: 1

3emad
3emad

Reputation: 238

I'd suggest to change the header to make it dynamic depend on the image mime-type:

header('Content-type: '.$row['mimetype']);

Upvotes: 0

Scuzzy
Scuzzy

Reputation: 12332

I'd suggest adding a content-length header before your output:

header("Content-length: " . strlen($content))

Upvotes: 0

Related Questions