problem with displaying images from mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ryanfern86goa
Forum Newbie
Posts: 16
Joined: Tue Sep 22, 2009 5:26 pm

problem with displaying images from mysql

Post by ryanfern86goa »

hi there
i have been trying to display all images from my table with click of a button .
i have created a form with one hidden with value=1 and when the button is clicked i need all the images to be displayed
I have 4 imges in the database and i get 4 image icons with raw data when i click the button but not the image
heres my php code
<? session_start(); ?>
<?php
$user1=$_SESSION["user1"];
$pass1=$_SESSION["pass1"];
$id=$_POST["display"];

@mysql_connect("localhost", sbc", "abcd") or die("Can not connect to

database: ".mysql_error());
@mysql_select_db('test') or die("Can not select the database: ".mysql_error());
echo"$id";
$query="select image from $user1 ";
$result=mysql_query($query);
echo("<table><tr>");
while($row = mysql_fetch_array($result)) {
echo("<td>");
$content=$row['image'];

echo("<img src=\"" . $row['image'] . "\"/>");

echo("</td>");
$id++;
if(($id % 6) == 0) echo("</tr>\n<tr>");
}
echo("</tr></table>");
?>
my table consist of two coloumns. 1) id 2) image
if this is wrong can anyone help me to display all my images in the table
any help would be appreciated,
thanks
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: problem with displaying images from mysql

Post by onion2k »

When you put an image on a page do you open up the file and paste the content in? No. Well, that's what you're doing here. It's never going to work like that.

I suggest you rethink your approach. Put the path to the image in the HTML, not the image itself.
charlesandrews
Forum Newbie
Posts: 1
Joined: Wed Sep 30, 2009 9:15 am

Re: problem with displaying images from mysql

Post by charlesandrews »

Can you give us a link to the running code so we can see exactly what is happening?
ryanfern86goa
Forum Newbie
Posts: 16
Joined: Tue Sep 22, 2009 5:26 pm

Re: problem with displaying images from mysql

Post by ryanfern86goa »

hi there
thanks for ur interest.
i am running it on my pc .. the result what i see when i press the show image button 4 empty image icon since there are 4 images in the table
followed by this for each image icon
÷ùtÏåàæØŸSîŽY5^t3ïó þéŸËÁͰ;>§Ý(œ‰š£ÅûüÀ¿ºgòðslÏ©÷IË"æ­çG>ÿ�0¯î™ü¼Û³ê}ÑË"æ¯çC>ÿ�0¯î™ü¼Û³ê}ÑË"f±ó£Ÿ¿˜W÷L~^›`v}Oº9dLÕ¼égïæýÓ—ƒ› v}OºN[5‡,ÿ�üÄ¿¹còð¼Û³ê}Òrع«yÔÏÿ�Ì+û–?/6@ìúŸtrعü–uxüÄ¿¹còð¼Ù³ê}ÑËbæ°é9t\Ö>uø…üÆ¿¹còðsdÏ©÷G-‹ŸÉcçcˆ_Ìkû–?/5ÀìúŸtrعªó±Ä?æ5ýË—…渟SîŽ]5o;Cþd_ܱùxNkÙõ>èåÑsUçcˆÌ‹û–?/5ÀìúŸtrعªó±Ä?æ5ýË—ƒšàv}Oº9t\ÕüëñùrÇåá9²gÔû£—EÍ_ο¿˜×÷,~^lÙõ>èå±sWó¯Ä/æ5ýË—ƒ› v}Oº9t\ÕyÖâóþ^K—ƒ› v}Oº9l\þJÞu¸‡üÆ¿¹còðsdÏ©÷G-‹š·~"u÷È¿¹còðsdÏ©÷G-‹š–™6{ç¥"wñã’]ŒÜMëÎ`ùírÞ:{;à·}ebN|B˜vÅõUQNUbQ|*:p!clWӁ Ú}˜­§Em8­§aÉ« 7À…N"ÀÃ ‘k%EÔܦŸekR–„¼% =Ò5x[¦×¡9šUiÃÓV:p$XiÀ•VœWӁ"¶ŒY`J²Ñ á«O³‘_N ¡[NªÚ0%R¯²ÌüÜp…uø–Ÿõâ¾¶×÷(ÇMg|¾µ‹7ñ bÛ•Ub0¨XÛ ‘`S…J­lYU°!clV·³EÙhfS½¡–‹à™ZÀS‡­[T˜œl'5§õ)¡Á/ä¸4_ ½ºâÙÁA¥p?6kó¢F;ò æòk}æ8ÕÉåó»S#ÃÓŽçO_Lt֐¬¾µ›®ªE“¦þ‘õc•ZT\ލ¶.±Ó À‘C¿*jªóxk(¦Í2ôÖ_XðJ’Ò¿Ã6Iëx(fEJ~Ë3óqÊ¢–>X4×çyAçT#ClÊvžÊ¹ÃZµA¦Á`|Û‹ÛÇaB+ºEkK³¢¸[ÄèQ+ùŸ"e*æh„ت×ÉJÂþ 6•)I(¹µÈ ½ë6Rw~iºÚiUmōÃT¦ïóCØUL€—bž[ˆp<\ I±×©w¿®øé o»Ñbs“²Züïæ]W÷:õ;ù˜`öèŽrvAm󽙯sM§{üÌ4ðFiÞ‰yÉÙÊ—W™ªŒ†›j’Ð]B²Iu#kÜãv¸–phh*›Ÿ}õ̧O¯ï²ÚU»?*fHÔµk¥RkL¦SÐ]¿V•e)&دiYòf%]㎝~š”Òœyn.jS{ˆø¡:No©×â×¥<-¦*“ÉŒÒw¡ ô>LkY Ã
HavokDelta6
Forum Newbie
Posts: 16
Joined: Mon Sep 28, 2009 4:11 pm

Re: problem with displaying images from mysql

Post by HavokDelta6 »

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Last edited by HavokDelta6 on Tue Mar 20, 2012 5:56 am, edited 1 time in total.
ryanfern86goa
Forum Newbie
Posts: 16
Joined: Tue Sep 22, 2009 5:26 pm

Re: problem with displaying images from mysql

Post by ryanfern86goa »

hi Havok

thanks for ur reply.. can you please let me know wats wrong with this code or what i need to add

heres my code
html form to display image

<form action="display.php" method="post">
<input name="display" value="1" type="hidden">
<input value="Display image" type="submit"></form>

insertimage.php
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {

// Temporary file name stored on the server
$tmpName = $_FILES['image']['tmp_name'];

// Read the file
$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);


// Create the query and insert
// into our database.
$query1 = "insert into $user1";
$query1 .= "(image) VALUES ('$data')";
$result1 = mysql_query($query1);

// Print results
print "Thank you, your file has been uploaded 2.".$user1;
mysql_close($connection);

display.php
$id=$_POST["display"];

@mysql_connect("localhost", "abc", "abcd") or die("Can not connect to database: ".mysql_error());
@mysql_select_db('test') or die("Can not select the database: ".mysql_error());
echo"$id";
$query="select image from $user1 ";
$result=mysql_query($query);
echo("<table><tr>");
while($row = mysql_fetch_array($result)) {
echo("<td>");
$content=$row['image'];

echo("<img src=\"" . $row['image'] . "\"/>");

echo("</td>");
$id++;
if(($id % 6) == 0) echo("</tr>\n<tr>");
}
echo("</tr></table>");
?>

i tried adding header('Content-type: image/jpg'); before echo("<img src=\"" . $row['image'] . "\"/>"); line and it gives error header already sent
thanks
raysance
Forum Newbie
Posts: 5
Joined: Tue Sep 29, 2009 1:25 pm

Re: problem with displaying images from mysql

Post by raysance »

this error "header already sent" is obvious because before header is executed echo is done. insert header at the starting of the page before any output.
and i think your query is wrong.
you must search for the exact image from the table

For eg:
$sql = mysql_query("SELECT image FROM table WHERE id='$user_id'");
$row = mysql_fetch_array($sql);
$image = $row['image'];
echo "<img src='$image'>";

this should work..
ryanfern86goa
Forum Newbie
Posts: 16
Joined: Tue Sep 22, 2009 5:26 pm

Re: problem with displaying images from mysql

Post by ryanfern86goa »

hi Ray
thanks for your reply.. i am trying to display all images from a table.. i have been trying for days and nothing works..can you help

thanks
raysance
Forum Newbie
Posts: 5
Joined: Tue Sep 29, 2009 1:25 pm

Re: problem with displaying images from mysql

Post by raysance »

no probs i'll write the complete code

suppose you have a table user_images with 2 columns namely id and image.
So now we have the table as
Table name: user_images
id image
1 pic1.jpg
2 pic2.jpg
3 pic3.jpg
4 pic4.jpg

The php code will be:
$sql = mysql_query("SELECT * FROM user_images WHERE 1 ORDER BY id");
while($row = mysql_fetch_array($sql))
{
$picture = $row['image'];
echo "<img src='$picture'><br />";
}

The above code will work for sure and <br /> is used to show one image in one line. You can modify the code as per your requirements. Hope it helps.
ryanfern86goa
Forum Newbie
Posts: 16
Joined: Tue Sep 22, 2009 5:26 pm

Re: problem with displaying images from mysql

Post by ryanfern86goa »

thanks raysance very much... i really deeply appreciate ur help.

i am still having problems... it does display all images but in raw format. I tried with adding header("content-type:image/jpg"); before the echo but it gives me the path of the script instead..

waiting for ur reply

ryan
raysance
Forum Newbie
Posts: 5
Joined: Tue Sep 29, 2009 1:25 pm

Re: problem with displaying images from mysql

Post by raysance »

hi ryan
i am no getting what exactly you mean by raw format.
Well, You can use all attributes like width, height, alt, align inside the echo statement.

echo "<img src='$picture' width='50' height='50' alt='new image' align='left' />";
satishbhawra42
Forum Newbie
Posts: 2
Joined: Thu Oct 01, 2009 2:50 pm

Re: problem with displaying images from mysql

Post by satishbhawra42 »

i have created a form with one hidden with value=1 and when the button is clicked i need all the images to be displayed
I have 4 imges in the database
ryanfern86goa
Forum Newbie
Posts: 16
Joined: Tue Sep 22, 2009 5:26 pm

Re: problem with displaying images from mysql

Post by ryanfern86goa »

hi raysance

sorry for not being more clear... when i run the script i get this kind of data with small missing pic icon in front. i have four pics in my mysql table and i get an missing pic icon followed by this four times.. i presume its printing the image but not in the jpg format.

missing pic icon 1
÷ùtÏåàæØŸSîŽY5^t3ïó þéŸËÁͰ;>§Ý(œ‰š£ÅûüÀ¿ºgòðslÏ©÷IË"æ­çG>ÿ�0¯î™ü¼Û³ê}ÑË"æ¯çC>ÿ�0¯î™ü¼Û³ê}ÑË"f±ó£Ÿ¿˜W÷L~^›`v}Oº9dLÕ¼égïæýÓ—ƒ› v}OºN[5‡,ÿ�üÄ¿¹còð¼Û³ê}Òrع«yÔÏÿ�Ì+û–?/6@ìúŸtrعü–uxüÄ¿¹còð¼Ù


missing pic icon 2
³ê}ÑËbæ°é9t\Ö>uø…üÆ¿¹còðsdÏ©÷G-‹ŸÉcçcˆ_Ìkû–?/5ÀìúŸtrعªó±Ä?æ5ýË—…渟SîŽ]5o;Cþd_ܱùxNkÙõ>èåÑsUçcˆÌ‹û–?/5ÀìúŸtrعªó±Ä?æ5ýË—ƒšàv}Oº9t\ÕüëñùrÇåá9²gÔû£—EÍ_ο¿˜×÷,~^lÙõ>èå±sWó¯Ä/æ5ýË—ƒ› v}Oº9t\ÕyÖâóþ^K—ƒ›


missing pic icon 3
v}Oº9l\þJÞu¸‡üÆ¿¹còðsdÏ©÷G-‹š·~"u÷È¿¹còðsdÏ©÷G-‹š–™6{ç¥"wñã’]ŒÜMëÎ`ùírÞ:{;à·}ebN|B˜vÅõUQNUbQ|*:p!clWӁ Ú}˜­§Em8­§aÉ« 7À…N"ÀÃ ‘k%EÔܦŸekR–„¼% =Ò5x[¦×¡9šUiÃÓV:p$XiÀ•VœWӁ"¶ŒY`J²Ñ á«O³‘_N ¡[NªÚ0%R¯²ÌüÜp…u


missing pic icon 4
ø–Ÿõâ¾¶×÷(ÇMg|¾µ‹7ñ bÛ•Ub0¨XÛ ‘`S…J­lYU°!clV·³EÙhfS½¡–‹à™ZÀS‡­[T˜œl'5§õ)¡Á/ä¸4_ ½ºâÙÁA¥p?6kó¢F;ò æòk}æ8ÕÉåó»S#ÃÓŽçO_Lt֐¬¾µ›®ªE“¦þ‘õc•ZT\ލ¶.±Ó À‘C¿*jªó

thanks again ray
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: problem with displaying images from mysql

Post by onion2k »

I told you that your current approach won't work in the second post in this thread. Why are you ignoring that and carrying on with it? You need to think about what your script is doing.

Hint: Putting the image data into the HTML is not going to work. Ever. Your HTML should only contain HTML.
Post Reply