Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Images

How to view image from DB.

images php coding issues or problems here.

Moderators: macek, egami, gesf

How to view image from DB.

Postby shnkrsv » Sun Oct 30, 2011 10:57 pm

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.
User avatar
shnkrsv
New php-forum User
New php-forum User
 
Posts: 24
Joined: Sun Oct 30, 2011 10:48 pm
Location: Coimbatore

Re: How to view image from DB.

Postby rupali » Mon Oct 31, 2011 12:52 am

<?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.

?>
rupali
New php-forum User
New php-forum User
 
Posts: 140
Joined: Thu Oct 13, 2011 10:32 pm
Location: Chandigarh, INDIA

Re: How to view image from DB.

Postby shnkrsv » Mon Oct 31, 2011 2:23 am

Thanks Rupali.. :)

I'll try this and reply u soon.
User avatar
shnkrsv
New php-forum User
New php-forum User
 
Posts: 24
Joined: Sun Oct 30, 2011 10:48 pm
Location: Coimbatore

Re: How to view image from DB.

Postby shnkrsv » Mon Oct 31, 2011 2:31 am

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 3346 times
User avatar
shnkrsv
New php-forum User
New php-forum User
 
Posts: 24
Joined: Sun Oct 30, 2011 10:48 pm
Location: Coimbatore

Re: How to view image from DB.

Postby rupali » Mon Oct 31, 2011 2:34 am

put a comment on header tag and check in the database the path for images should be correct.

Now, try
rupali
New php-forum User
New php-forum User
 
Posts: 140
Joined: Thu Oct 13, 2011 10:32 pm
Location: Chandigarh, INDIA

Re: How to view image from DB.

Postby shnkrsv » Mon Oct 31, 2011 2:38 am

Ya.. Will get back u..
User avatar
shnkrsv
New php-forum User
New php-forum User
 
Posts: 24
Joined: Sun Oct 30, 2011 10:48 pm
Location: Coimbatore

Re: How to view image from DB.

Postby shnkrsv » Mon Oct 31, 2011 2:45 am

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.?
User avatar
shnkrsv
New php-forum User
New php-forum User
 
Posts: 24
Joined: Sun Oct 30, 2011 10:48 pm
Location: Coimbatore

Re: How to view image from DB.

Postby rupali » Mon Oct 31, 2011 2:51 am

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?
rupali
New php-forum User
New php-forum User
 
Posts: 140
Joined: Thu Oct 13, 2011 10:32 pm
Location: Chandigarh, INDIA

Re: How to view image from DB.

Postby shnkrsv » Mon Oct 31, 2011 2:54 am

Wait, i'll send my code and db in package..
User avatar
shnkrsv
New php-forum User
New php-forum User
 
Posts: 24
Joined: Sun Oct 30, 2011 10:48 pm
Location: Coimbatore

Re: How to view image from DB.

Postby shnkrsv » Mon Oct 31, 2011 3:02 am

Attachment with full code and DB.
full.rar
(2.07 KiB) Downloaded 88 times
User avatar
shnkrsv
New php-forum User
New php-forum User
 
Posts: 24
Joined: Sun Oct 30, 2011 10:48 pm
Location: Coimbatore

Re: How to view image from DB.

Postby shnkrsv » Mon Oct 31, 2011 3:20 am

Is ter any solutions found? :(
User avatar
shnkrsv
New php-forum User
New php-forum User
 
Posts: 24
Joined: Sun Oct 30, 2011 10:48 pm
Location: Coimbatore

Re: How to view image from DB.

Postby rupali » Mon Oct 31, 2011 3:27 am

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.
Attachments
full.rar
(13.77 KiB) Downloaded 79 times
rupali
New php-forum User
New php-forum User
 
Posts: 140
Joined: Thu Oct 13, 2011 10:32 pm
Location: Chandigarh, INDIA

Re: How to view image from DB.

Postby shnkrsv » Mon Oct 31, 2011 3:30 am

Ya, k.. I'll try and get back to you.. :)
User avatar
shnkrsv
New php-forum User
New php-forum User
 
Posts: 24
Joined: Sun Oct 30, 2011 10:48 pm
Location: Coimbatore

Re: How to view image from DB.

Postby shnkrsv » Mon Oct 31, 2011 3:40 am

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..
User avatar
shnkrsv
New php-forum User
New php-forum User
 
Posts: 24
Joined: Sun Oct 30, 2011 10:48 pm
Location: Coimbatore

Re: How to view image from DB.

Postby rupali » Mon Oct 31, 2011 3:42 am

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.
rupali
New php-forum User
New php-forum User
 
Posts: 140
Joined: Thu Oct 13, 2011 10:32 pm
Location: Chandigarh, INDIA

Re: How to view image from DB.

Postby shnkrsv » Mon Oct 31, 2011 3:47 am

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.
User avatar
shnkrsv
New php-forum User
New php-forum User
 
Posts: 24
Joined: Sun Oct 30, 2011 10:48 pm
Location: Coimbatore

Re: How to view image from DB.

Postby rupali » Mon Oct 31, 2011 3:49 am

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
Last edited by rupali on Mon Oct 31, 2011 3:53 am, edited 1 time in total.
rupali
New php-forum User
New php-forum User
 
Posts: 140
Joined: Thu Oct 13, 2011 10:32 pm
Location: Chandigarh, INDIA

Re: How to view image from DB.

Postby rupali » Mon Oct 31, 2011 3:51 am

my pleasure shnkrsv . i am happy with my work that works for you :)
rupali
New php-forum User
New php-forum User
 
Posts: 140
Joined: Thu Oct 13, 2011 10:32 pm
Location: Chandigarh, INDIA

Re: How to view image from DB.

Postby shnkrsv » Mon Oct 31, 2011 3:55 am

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
User avatar
shnkrsv
New php-forum User
New php-forum User
 
Posts: 24
Joined: Sun Oct 30, 2011 10:48 pm
Location: Coimbatore

Re: How to view image from DB.

Postby rupali » Mon Oct 31, 2011 3:59 am

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.
rupali
New php-forum User
New php-forum User
 
Posts: 140
Joined: Thu Oct 13, 2011 10:32 pm
Location: Chandigarh, INDIA

Re: How to view image from DB.

Postby shnkrsv » Mon Oct 31, 2011 4:05 am

Ya.. thank you.. and don't be sorry for my name.. :)
Hope to find you to solve my next queries...
User avatar
shnkrsv
New php-forum User
New php-forum User
 
Posts: 24
Joined: Sun Oct 30, 2011 10:48 pm
Location: Coimbatore

Re: How to view image from DB.

Postby rupali » Mon Oct 31, 2011 4:22 am

sure i'll.
rupali
New php-forum User
New php-forum User
 
Posts: 140
Joined: Thu Oct 13, 2011 10:32 pm
Location: Chandigarh, INDIA

Re: How to view image from DB.

Postby rupali » Mon Oct 31, 2011 10:41 pm

extremely wonderful designs of all the websites especially the logo of travelmate.
so, you are designer cum developer or developer cum designer?
rupali
New php-forum User
New php-forum User
 
Posts: 140
Joined: Thu Oct 13, 2011 10:32 pm
Location: Chandigarh, INDIA

Re: How to view image from DB.

Postby shnkrsv » Mon Oct 31, 2011 10:56 pm

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... :)
User avatar
shnkrsv
New php-forum User
New php-forum User
 
Posts: 24
Joined: Sun Oct 30, 2011 10:48 pm
Location: Coimbatore

Re: How to view image from DB.

Postby rupali » Mon Oct 31, 2011 11:03 pm

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 :)
rupali
New php-forum User
New php-forum User
 
Posts: 140
Joined: Thu Oct 13, 2011 10:32 pm
Location: Chandigarh, INDIA

Next

Return to PHP coding => Images

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.