Page 1 of 2

How to view image from DB.

Posted: Sun Oct 30, 2011 10:57 pm
by shnkrsv
Hai All,

Am a new to PHP, but i manage to insert an image in to my db. Now now i dont find an way to display that image in my browser.
Here my code.

<?php
$host = 'localhost';
$user = 'root';
$pw = '';
$db = 'demo';

mysql_connect($host,$user,$pw);
mysql_select_db($db);

$sql = "select img from first";//img is the field name with type:blob where the images are saved.

$result = mysql_query($sql) or die('Bad query at 12!'.mysql_error());

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{


$dbimg1=$row['img'];
header("Content-Type: image/png");//db contains only .png image files.
echo $dbimg1;
echo "<br/>";


}


?>

And am getting the error;
The image “http://localhost/demo/img.php” cannot be displayed because it contains errors.
Please someone find me solution.

Re: How to view image from DB.

Posted: Mon Oct 31, 2011 12:52 am
by rupali
<?php
$host = 'localhost';
$user = 'root';
$pw = '';
$db = 'demo';

mysql_connect($host,$user,$pw);
mysql_select_db($db);

$sql = "SELECT img FROM first";//img is the field name with type:blob where the images are saved.

$result = mysql_query($sql) or die('Bad query at 12!'.mysql_error());

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "<img src=".$row['img']." height='50' width='50'><br/>";
}
//header("Content-Type: image/png");//db contains only .png image files.

?>

Re: How to view image from DB.

Posted: Mon Oct 31, 2011 2:23 am
by shnkrsv
Thanks Rupali.. :)

I'll try this and reply u soon.

Re: How to view image from DB.

Posted: Mon Oct 31, 2011 2:31 am
by shnkrsv
I have tried it but still i cont view my images....
My Code :

<?php


$host = 'localhost';
$user = 'root';
$pw = '';
$db = 'demo';


mysql_connect($host,$user,$pw);

mysql_select_db($db);

$sql = "select * from first";

$result = mysql_query($sql) or die('Bad query at 12!'.mysql_error());

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
header("Content-type : images/png");
echo "<img src=".$row['img']." height='50' width='50'><br/>";

}



?>

But am getting the result as boxes.. Is ter any conversion needed b4 it get displayed...
Please help me to finding out the solutions for this..

Attachment with screenshot.
NO IMAGE DISPLAY.jpg
Screen Shot
NO IMAGE DISPLAY.jpg (227.97 KiB) Viewed 10397 times

Re: How to view image from DB.

Posted: Mon Oct 31, 2011 2:34 am
by rupali
put a comment on header tag and check in the database the path for images should be correct.

Now, try

Re: How to view image from DB.

Posted: Mon Oct 31, 2011 2:38 am
by shnkrsv
Ya.. Will get back u..

Re: How to view image from DB.

Posted: Mon Oct 31, 2011 2:45 am
by shnkrsv
Ya, i have tried but still have the same issue... Wil ter be any problem in uploading the image?

I'll send the image insert code now..

Code :

//insert.php

<?php

$hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost
$db_user = "root"; // change to your database password
$db_password = ""; // change to your database password
$database = "demo"; // provide your database name
$db_table = "first"; // leave this as is


$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database,$db);
?>

<html>
<head>
</head>
<body>
<h2>Insert Operation</h2>
<ul style="list-style-type:none;">
<li><a href="main.php">Home</a></li>
<li><a href="insert.php">Insert</a></li>
<li><a href="update.php">Update</a></li>
<li><a href="delete.php">Delete</a></li>
</ul>





<?php
if (isset($_REQUEST['submit']))
{


$name2=$_POST['name1'];
$url2=$_POST['url1'];
$sprice2=$_POST['sprice1'];
$eprice2=$_POST['sprice1'];
$des=$_POST['desc1'];
$img = $_POST['img1'];
$image_name = stripslashes($img);




if(!$img)
echo "This Not an Image";
else
{

$sql="INSERT INTO first VALUES('', '$name2','$url2','$sprice2', '$eprice2', '$des', '$img', '$image_name')";

if(!$result = mysql_query($sql ,$db))
echo "Problem Uploading Image File";
else
{
$lastid=mysql_insert_id();
echo "Image Uploaded Successfully.<p />Your Image :<p /><img src=get.php?id=$lastid>";
}
}

}


?>



<form action="" method="post" enctype="maltipart/form-data">
<table>
<tr>
<td>Name : </td><td><input type="text" name="name1"></td>
</tr>
<tr>
<td>URL : </td><td><input type="text" name="url1"></td>
</tr>
<tr>
<td>Starting Price : </td><td><input type="text" name="sprice1"></td>
</tr>
<tr>
<td>Ending Price : </td><td><input type="text" name="eprice1"></td>
</tr>
<tr>
<td>Description : </td><td><textarea rows="6" cols="40" name="desc1"></textarea></td>
</tr>
<tr><td>Upload An Image:</td><td><input type="file" name="img1" size="20"></td></tr>
<tr>
<td><input type="submit" value="Submit" name="submit"></td>
<td><input type="reset" value="Reset" name="clear"></td>
</table>

</form>

<br />


</body>
</html>

//get.php

<?php

$hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost
$db_user = "root"; // change to your database password
$db_password = ""; // change to your database password
$database = "demo"; // provide your database name
$db_table = "first"; // leave this as is


$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database,$db);

//$id=addslashes($_REQUEST['id']);

$id = intval($_GET['id']); // convert string to int

$row=mysql_query("SELECT * FROM $db_table WHERE id=$id");
$image=mysql_fetch_assoc($row);
$img=$image['img'];
//header("Content-type : image/png");
echo $img;
mysql_free_result($row);

?>


This is what i needed, after inserting the data the images should retrive in the same page, but am not getting so for, so first am tried to get my image viewed by simple code. but am not getting..

Is ter any issues in insert code of my insert.php file.?

Re: How to view image from DB.

Posted: Mon Oct 31, 2011 2:51 am
by rupali
please let me know that if you get successfully inserted the data in the database, then what are the values you get stored? can you please send me the records which at present in the database?

Re: How to view image from DB.

Posted: Mon Oct 31, 2011 2:54 am
by shnkrsv
Wait, i'll send my code and db in package..

Re: How to view image from DB.

Posted: Mon Oct 31, 2011 3:02 am
by shnkrsv
Attachment with full code and DB.
full.rar
(2.07 KiB) Downloaded 168 times

Re: How to view image from DB.

Posted: Mon Oct 31, 2011 3:20 am
by shnkrsv
Is ter any solutions found? :(

Re: How to view image from DB.

Posted: Mon Oct 31, 2011 3:27 am
by rupali
there is a spelling mistake in "multipart" thats why it is not inserting any record. now just try this only insert.php and let me know that you get the result or not.

i haven't done any change in get.php, so just try only insert.php file and don't remove the folder upload because its necessary to uploading the images you need a separate folder for that.

Re: How to view image from DB.

Posted: Mon Oct 31, 2011 3:30 am
by shnkrsv
Ya, k.. I'll try and get back to you.. :)

Re: How to view image from DB.

Posted: Mon Oct 31, 2011 3:40 am
by shnkrsv
yap, now the db is updated the image file... then how to get back to display the image..?

I dont understand why you used upload folder there.. am new to php am not so advanced..

Re: How to view image from DB.

Posted: Mon Oct 31, 2011 3:42 am
by rupali
because for uploading the images we always need a separate folder to store the path for images with every person record and in the database we just only need to display on web page.

Re: How to view image from DB.

Posted: Mon Oct 31, 2011 3:47 am
by shnkrsv
Oh k.. I understood... So everytime we need a folder to save our images, and as the same way we need to get back to view those images by ter names!
Thank you sooo much rupali.. :D.

Re: How to view image from DB.

Posted: Mon Oct 31, 2011 3:49 am
by rupali
this is the code for get.php

<?php
$host = 'localhost';
$user = 'root';
$pw = '';
$db = 'demo';

mysql_connect($host,$user,$pw);
mysql_select_db($db);

$sql = "SELECT img FROM first";//img is the field name with type:blob where the images are saved.

$result = mysql_query($sql) or die('Bad query at 12!'.mysql_error());

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "<img src=upload/".$row['img']." height='50' width='50'><br/>";
}
//header("Content-Type: image/jpg");//db contains only .png image files.

?>

For execution of this code
path is : http://localhost/full/get.php

Re: How to view image from DB.

Posted: Mon Oct 31, 2011 3:51 am
by rupali
my pleasure shnkrsv . i am happy with my work that works for you :)

Re: How to view image from DB.

Posted: Mon Oct 31, 2011 3:55 am
by shnkrsv
Have a great time... :D

And my name is not "shnkrsv" its just an id..
Am "shiva.."

Now i have to work to find how to display that image in my web page..
www.jananatech.net
www.vssgroups.com
www.sboapwa.com
www.diamondbookbank.com
www.jbbattery.com
www.srinithiprinters.com

This are some of my works..
Present work

www.travelmate.co.in

Have a great day rupali.. :D

Re: How to view image from DB.

Posted: Mon Oct 31, 2011 3:59 am
by rupali
sorry for the name, actually i have seen your id name thats why,
but now i get your name,
sure you will definitely try the code and that works.

Wonderful time to solve your query. Good day to you too.

Re: How to view image from DB.

Posted: Mon Oct 31, 2011 4:05 am
by shnkrsv
Ya.. thank you.. and don't be sorry for my name.. :)
Hope to find you to solve my next queries...

Re: How to view image from DB.

Posted: Mon Oct 31, 2011 4:22 am
by rupali
sure i'll.

Re: How to view image from DB.

Posted: Mon Oct 31, 2011 10:41 pm
by rupali
extremely wonderful designs of all the websites especially the logo of travelmate.
so, you are designer cum developer or developer cum designer?

Re: How to view image from DB.

Posted: Mon Oct 31, 2011 10:56 pm
by shnkrsv
Thank You... :) basically am owning my company.. Janana Technologies Solutions... and am a java developer cum web designer.. now getting into PHP..
Rupali is my trainer for PHP... :)

Re: How to view image from DB.

Posted: Mon Oct 31, 2011 11:03 pm
by rupali
That's wonderful to know about your own company and all the websites are designed extremely wonderful.
I am your trainer? no, please i am also a medium coder in php. i solved your query, its my work that appreciation for me.
Thank you so much :)