Re: [PHP] displaying image from MySQL DB using HTML/PHP



Bruce Gilbert wrote:
I am having some difficulty getting an image to display on a php that
I have added to MySQL DB.

Here is what I have tried so far....

in the MySQL DB I have a table called image_holder and the fields are
id,mimename,filecontents...filecontents field is set to a type of blob
using PHPMyAdmin and I have uploaded the image in MySQL.

In the PHP code I have:

[php]
<?php
$dbhost = 'hostaddress';
$dbuser = 'username';
$dbpass = 'password;
$dbcnx = @mysql_connect($dbhost,$dbuser,$dbpass);

if (!$dbcnx)
{
echo( "connection to database server failed!");
exit();
}
if (! @mysql_select_db("image_holder") )
{
echo( "Image Database Not Available!" );
exit();
}

DON'T USE REQUEST
only use it from where you expect it to be, in this case, $_GET
$img = $_REQUEST["img"];

I see you calling a table called images not image_holder as stated previously.
$result = @mysql_query("SELECT * FROM images WHERE id=" . $img . "");

if (!$result)
{
echo("Error performing query: " . mysql_error() . "");
exit();
}

you don't need a while loop here, get rid of it.
while ( $row = @mysql_fetch_array($result) )
{
$imgid = $row["id"];
$encodeddata = $row["mimetype"];
$title = $row['filecontents'];
}

here is the end, but I see no echoing headers or of the data????

you need to spit out headers for the given type of image and then echo the data and then exit.

$result = @mysql_query("SELECT * FROM images WHERE id=" . $img . "");
if (!$result) {
echo("Error performing query: " . mysql_error() . "");
exit();
}

if ( ( $row = @mysql_fetch_array($result) ) === FALSE ) {
header("Content-Type: {$row['mimetype']}");
echo $row['filecontents'];
exit();
}
?>

and in the HTML code

<center><img src="image.php?img=1" width="200" border="1" alt=""></center>

I am probably way off base, so need some help!


.



Relevant Pages

  • Re: mysql apache - page never finish loading
    ... my PHP pages don't want to finish loading. ... echo 'a'; ... Opera and IE behaves somewhat differently. ... All this happens whenver I connect to mysql. ...
    (comp.lang.php)
  • Re: php form info...
    ... when trying to search in the mysql statement. ... : echo $testvar; ... combine a variable containing an arbitrary string with a constant number. ... The results within php depend a great deal on the contents ...
    (comp.lang.php)
  • Re: php 4.4.2/ mysql 5.0.27 Combination Not working
    ... I upgraded some old versions of Apache and mysql, which worked fine, ... When moving the connection string after phpinfoor echo ... setup of Apache or php. ... Have changed the configuration settings in both php and mysql to ...
    (php.general)
  • Re: mysql apache - page never finish loading
    ... The problem is not lack of error handling, ... I was hoping that this was some kind of "known problem" when using PHP ... mysql, my PHP pages don't want to finish loading. ... echo 'a'; ...
    (comp.lang.php)
  • mysql apache - page never finish loading
    ... I am new to PHP, Apache and Mysql. ... echo 'a'; ... I am running Apache 2.2.2 and Mysql 5.0. ...
    (comp.lang.php)