How to view image from DB.
Moderators: egami, macek, gesf
- shnkrsv
- New php-forum User
- Posts: 27
- Joined: Sun Oct 30, 2011 10:48 pm
- Location: Coimbatore
- Contact:
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.
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.
<?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.
?>
$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.
?>
- shnkrsv
- New php-forum User
- Posts: 27
- Joined: Sun Oct 30, 2011 10:48 pm
- Location: Coimbatore
- Contact:
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.
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.
- shnkrsv
- New php-forum User
- Posts: 27
- Joined: Sun Oct 30, 2011 10:48 pm
- Location: Coimbatore
- Contact:
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.?
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.?
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.
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 147 times
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
<?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.
- shnkrsv
- New php-forum User
- Posts: 27
- Joined: Sun Oct 30, 2011 10:48 pm
- Location: Coimbatore
- Contact:
Have a great time...
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..

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

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