Page 1 of 1
problem with displaying images from mysql
Posted: Wed Sep 30, 2009 8:41 am
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
Re: problem with displaying images from mysql
Posted: Wed Sep 30, 2009 9:24 am
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.
Re: problem with displaying images from mysql
Posted: Wed Sep 30, 2009 9:25 am
by charlesandrews
Can you give us a link to the running code so we can see exactly what is happening?
Re: problem with displaying images from mysql
Posted: Wed Sep 30, 2009 9:58 am
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…JlYU°!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 Ã
Re: problem with displaying images from mysql
Posted: Wed Sep 30, 2009 10:23 am
by HavokDelta6
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Re: problem with displaying images from mysql
Posted: Wed Sep 30, 2009 11:08 am
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
Re: problem with displaying images from mysql
Posted: Wed Sep 30, 2009 1:36 pm
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..
Re: problem with displaying images from mysql
Posted: Wed Sep 30, 2009 4:04 pm
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
Re: problem with displaying images from mysql
Posted: Thu Oct 01, 2009 12:39 pm
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.
Re: problem with displaying images from mysql
Posted: Thu Oct 01, 2009 1:30 pm
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
Re: problem with displaying images from mysql
Posted: Thu Oct 01, 2009 2:01 pm
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' />";
Re: problem with displaying images from mysql
Posted: Thu Oct 01, 2009 2:55 pm
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
Re: problem with displaying images from mysql
Posted: Thu Oct 01, 2009 3:26 pm
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…JlYU°!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
Re: problem with displaying images from mysql
Posted: Fri Oct 02, 2009 2:51 am
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.