Forum Moderators: coopster

Message Too Old, No Replies

Dispalying image from mysql database

Dispalying image from mysql database using php

         

okonjiaustin

8:04 am on Feb 26, 2008 (gmt 0)

10+ Year Member



I want to specifically direct this problem to 'Jatar'

I have successfully insert image and other row data into mysql database.
The code is below:

<?php
$errmsg = "";
if (! @mysql_connect("localhost","root","")) {
$errmsg = "Cannot connect to database";
}
@mysql_select_db("adim");

if(isset($_REQUEST['submit']))
{

$imgtype=$_FILES['uploadfile']['type'];
$name=$_REQUEST['name'];
$address=$_REQUEST['address'];
$dateofbirth=$_REQUEST['dateofbirth'];

if($imgtype=="image/jpeg" $imgtype=="image/jpg" $imgtype=="image/pjpeg" $imgtype=="image/gif" $imgtype=="image/x-png" $imgtype=="image/bmp")
{

$image=$_FILES['uploadfile']['tmp_name'];
$fp = fopen($image, 'r');
$content = fread($fp, filesize($image));
$content = addslashes($content);
fclose($fp);
$sql="insert into img_tab1 (name,image,address,dateofbirth) values ('$name','$content','$address','$dateofbirth')";
$res=mysql_query($sql) or die (mysql_error());
}
}
?>
Now I want to retrieve the row data into an html page including the image, but after using the code below the row data failed to be displayed in an html page. Please I want you to look at this code and tell me what I have done wrong. This problem has been on for quite sometime now.
Thanks. This is the display code below:

<?php
$errmsg = "";
if (! @mysql_connect("localhost","root","")) {
$errmsg = "Cannot connect to database";
}
@mysql_select_db("adim");

$query = "SELECT image, image_type from img_tab1 where id=$id";
$result = @mysql_QUERY($query);

$data = @mysql_RESULT($result,0,"image");
$type = @mysql_RESULT($result,0,"image_type");

header( "Content-type: $type");
echo $data;

?>
Please go through and let me know where I went wrong.

Thanks

Austin

phparion

11:31 am on Feb 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



while you're programming you should not use @, let the function break to see any error thrown from the compiler.. e.g

$result = @mysql_QUERY($query);

$data = @mysql_RESULT($result,0,"image");
$type = @mysql_RESULT($result,0,"image_type");

convert the second part of the functions to the lower case i.e mysql_query() and mysql_result, additionally use

mysql_query() or die(mysql_error())

to see if any problem occurs with your query itself. At least when you are developing...

okonjiaustin

6:14 am on Feb 27, 2008 (gmt 0)

10+ Year Member



Hello phparion,
Thanks for your reply. I have made the corrections you suggested and this is the corrected version of the code:

<?php
$db = mysql_connect("localhost", "root", "");
mysql_select_db("adim",$db);

$query = "SELECT image, image_type from img_tab1 where id=$id";
$result = mysql_query($query) or die('Error, query failed');

$data = @mysql_result($result,0,"image");
$type = @mysql_result($result,0,"image_type");

echo ($data);

?>
And this is the result:
Error, query failed

So, where do I go from here? Hoping to hear from you.

Austin

phparion

9:20 am on Feb 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



change this

$query = "SELECT image, image_type from img_tab1 where id=$id";
$result = mysql_query($query) or die('Error, query failed');

to this

$query = "SELECT image, image_type from img_tab1 where id=$id";
echo $query ."<br>";
$result = mysql_query($query) or die(mysql_error());

check your query shape i.e if $id exists in the query then check the mysql error. show me that.. btw i guess you are either giving wrong column name or $id is not put in the query.. but lets see what error do you get...

okonjiaustin

3:54 pm on Feb 27, 2008 (gmt 0)

10+ Year Member



Thanks phparion for your concern.

I have actually made the corrections and included the real column names in the database, the code is below:

<?php
$db = mysql_connect("localhost", "root", "");
mysql_select_db("adim",$db);

$query = "SELECT name,image,address,dateofbirth from img_tab1 where id=1";
echo $query ."<br>";
$result = mysql_query($query) or die(mysql_error());

$data = @mysql_result($result,0,"image");
$type = @mysql_result($result,0,"image_type");

echo ($data);

?>

When I executed the query the result was an unbelievable junk that filled all the page; see a sample below:

SELECT name,image,address,dateofbirth from img_tab1 where id=1
JFIFddDuckyP&Adobed Z&06ۄ      ^, 6547 3@!#$%  !1AQaq"2rtBR#3suтS6 bc$@C4T5%E!1AQaq 0@  stf*Oa1Xq$R^KWrdaaaaw'~,s MX+$K0p? -p%@b4hg,0W-*qP$he+$K0pJL' +W;h >[Ef ) `oI`wÉ"*\5yLj0<h7AA/:@КrfO!dU`8)/x¥Á`E{0<h łY,ĵ5HYXU +$K0pX^¸(3p "p2=@( %› z +KpA 2*ܓK,5 DoEy!\J¦ 2 n9? u7qb\36Q%Hwh_`+RX1ސ#HkDM2j IȈ 0+(aP@n`RL~BbA cti9¦5춻0a_U'GGxY-\j" "61V]ʼK[> -ntnyۛ+ j-3Տ/~S*S n;O-+}W,\{0DV\jvn.>s¦, _rI~9c[.H5]V8>m_U;u}WmwD\(`FN kmdE-)%R@9$Ej9y͡#4e7F "e6

Thanks.

Austin