Getting an image from a database - php

I have a getimage.php containing
<?php
$id = $_GET['id'];
// do some validation here to ensure id is safe
$link = mysql_connect("localhost", "user", "password");
mysql_select_db("database");
$sql = "SELECT photo FROM property_photo WHERE id=$id";
$result = mysql_query("$sql");
$row = mysql_fetch_assoc($result);
mysql_close($link);
header("Content-type: image/jpeg");
echo $row['property_photo'];
?>
and my index.php
<div class="bloco"> <img src="getImage.php?id=1" ?>
<div>Description</div>
</div>
<div class="insert"></div>
</div>
i can't see the photo, It won't display or it displays as a blank box with a blue question mark.

In your query you are selecting the field 'photo' but when you come to echo the field you actually echo the name of the table:
echo $row['property_photo'];
So I believe your actually getting a hidden fatal error instead of the image saying - unknown index 'property_photo'
try
echo $row['photo'];
instead.

use ob_clean() before header,
it may solves your problem

Related

php mySQL how to show images, problems on img src

I've been working on this for four hours and I still have no clue, so I came here to seek help. I'm learning PHP and mySQL and one of the way I learn is to learn from open source projects. Today I'm trying to understand an open source project and I have some problems. Here is the link to the open source project: https://github.com/markpytel/Printstagram Basically, it has something like the following, let's call it profileinfo.php (name of this file is pretty deceiving, it is a page that shows images uploaded by different users)
<?php
$sql="SELECT pid,poster, pdate FROM photo WHERE poster='$myusername' OR pid
in (select pid from tag where taggee='$myusername') OR pid in (select pid
from ingroup natural join shared where username='$myusername' and username
!= ownername) ORDER BY pdate desc";
$result=mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo "<hr>";
echo "Posted by: " . $row["poster"]. " Time: " . $row["pdate"]."<br>";
// pid is each photo's unique id in the database
$pidrow=($row["pid"]);
?>
</head>
<body>
<form action="listsingleREV.php?pidrow=<?php echo $pidrow; ?>" method="POST">
<input type="submit" id="pidrow" value="View this image" />
</form>
</body>
</html>
If you click on the button that says "view this image", image uploaded by users will appear. The first question I have is: What's the meaning of the question mark in the image src. I understand that there is a PHP tag in the img src, but I don't understand why there is a question mark between listsingleREV.php and pidrow.
listsingleREV.php looks like this:
<?php
session_start();
$pidrow=$_GET['pidrow'];
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("printstagram");
$sql = "SELECT pid FROM photo WHERE pid=$pidrow";
$result = mysql_query($sql);
?>
<?php
while($row = mysql_fetch_array($result)) {
?>
<img src="imageview.php?pid=<?php echo $row["pid"]; ?>" /><br/>
<?php
}
?>
imageview.php looks like this:
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("printstagram") or die(mysql_error());
if(isset($_GET['pid'])) {
$sql = "SELECT image FROM photo WHERE pid=" . $_GET['pid'];
$result = mysql_query("$sql") or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>" . mysql_error());
$row = mysql_fetch_array($result);
header("content-type: image/jpeg");
echo $row["image"];
}
mysql_close($conn);
?>
Of course, if a user has to click on a button to see each image, it is very inconvenient. So I decide to give myself some practice and modify the code(it is Apache license so I can do it)such that images will be automatically presented on profileinfo.php without the need to click on a button. Since imageview.php and listsingleREV.php show the images, I tried to substitute the form in profileinfo.php with these two files. I worked on it for four hours without achieving my goal. Can someone tell me the correct way to show the images on profileinfo.php without the need to click on the button?
As it appears in imageview.pho the $row[pid] contain the link of your image in photo table so add this line to profileinfo.php under the while loop in the place you want it to display
<img src="<?php echo". $row["pid"]."; ?>">

retrieve image from database using mysqli php 7

I am trying to retrieve a png image file from the database
Here is the call from within the <img> tag inside body:
<img src="..\BankLogin\man.php?id=2" style="width:128px;height:150px">
Here's the man.php file:
<?php
$link = mysqli_connect("localhost","root","","images");
$imgId = $_GET['id'];
if (!empty($imgId)) {
$sqliCommand = "SELECT image FROM images WHERE id = $imgId";
$result = mysqli_query($sqliCommand,$link);
$row = mysqli_fetch_assoc($result);
mysqli_close($link);
header("Content-type: image/png");
echo $row['image'];
}
?>
On running the code i just get an image frame with an 'unloaded image'(am i saying it correct?).I am pretty sure something is wrong in the man.php file, maybe in echo $row['image']. I am not sure how to go about making it right. Any help with this would be great.
The function mysqli_close should be called after the image data is echoed. This is because it destroys the result sets.
Also please fix the SQL Injection vulnerability:
$imgId = (int)$_GET['id'];
If you want to retrieve the image which is stored as BLOB type in phpmyadmin you have to echo it as follows.
echo '<img src="data:image/jpeg;base64,'.base64_encode( $rows['image'] ).'"/>'
Example:
To Retrieve the BLOB image from the DB you have to do like this.
<?php
$db = mysqli_connect("localhost","root","","dbname"); //keep your db name
$query = "SELECT * FROM image WHERE id = $id";
$sth = $db->query($query);
$fetch=$sth->fetch_assoc();
echo '<img src="data:image/jpeg;base64,'.base64_encode( $fetch['image'] ).'"/>';
?>
For Inserting the image you need to follow the procedure like this So that if you encode it as base 64 you can retrieve the image perfectly without any error.
<?php
$conn = mysqli_connect("localhost","root","","DbName"); //keep your db name
$single_image = addslashes(file_get_contents($_FILES['images']['tmp_name']));
//U have to keep your DB table column name for insertion. I keep image type Blob.
$query = "INSERT INTO image (image) VALUES('".$single_image."')";
$SQL = mysqli_query($conn, $query);
?>

cant able to get the blob stored image into image format(MySql AND php)?

I am trying to get the image which is stored in the blob format in Mysql Database.I am using the following code.
index.php
<img src='image.php?questionid=questionid&question=question1&answer=answer1&author=myname' />
image.php
<?php
require_once('dbconfiguration.php');
$sql="SELECT image FROM tablename WHERE questionid='{$_GET['questionid']}' and question='{$_GET['question']}' and answer='{$_GET['answer']}' and author='{$_GET['author']}'";
$STH = $DBH->query($sql);
$STH->setFetchMode(PDO::FETCH_ASSOC);
$row = $STH->fetch();
ob_clean();
header("content-type: image/jpg") ;
echo $row['image'] ;
?>
In index.php I didn't get any image.So When I enter the URL for image.php I am getting the following error in forefox.
The image “URL” cannot be displayed because it contains errors.
I am using php5.3 and mysql 5.1.36 .
What I did wrong.I went through almost all forums and no clues.Please help me on this.
It should be:
<?php
require_once('dbconfiguration.php');
$sql="SELECT image FROM tablename WHERE questionid='{$_GET['questionid']}' and question='{$_GET['question']}' and answer='{$_GET['answer']}' and author='{$_GET['author']}'";
$STH = $DBH->query($sql);
$STH->setFetchMode(PDO::FETCH_ASSOC);
$row = $STH->fetch();
ob_clean();
header("content-type: image/jpg") ;
echo $row['image'] ;
?>
As your database field is called image not Efaq_image

php display image

I am not able to display image using this code. Could any one help me in correcting?
<?php
include("connect.php");
$sql="select * from profile where userid=3";
$row=mysql_query($sql);
header("Content-type: image/jpeg");
echo $row['image'];
?>
Try
<?php
include("connect.php");
$sql = "SELECT * FROM profile WHERE userid='3'";
$row = mysql_fetch_assoc(mysql_query($sql));
echo '<img src="'.$row['image'].'" />';
?>
Depends how your information is stored in the database. If you have stored the path to a picture, this will be the right way. If you have stored the whole picture, this must be done another way.
Have you tried this way:-
echo "<img src=\"".$row['image']."\">";

Getting information from image using mysql database

I have a home page where database value are showing (e.g. a image and few information)
My problem is whenever i click on the image it jumps to another page and show all the pictures that are store in database,but i want the clicked one.
Here is my 1st page home_page.php code
<?php
$db = mysqli_connect("localhost", "root", "", "registration_data");
$sql = "SELECT * FROM add_data";
$result = mysqli_query($db,$sql);
while($row = mysqli_fetch_array($result)){
$image = $row['image'];
echo "<div id='img_div'>";
?><?php echo "<img src='images/".$row['image']."' >";?><?php
echo "<p>Description: ".$row['add_description']." </p>";
echo "<p>Cetegory: ".$row['catagory']. "</p>";
echo "</div>";
}
?>
And this is my second page where i want to show only clicked image information.(Details.php)
<?php
$image = intval($_GET["image"]);
$db = mysqli_connect("localhost", "root", "", "registration_data");
$sql = "SELECT * FROM add_data where 'id' = $image ";
$result = mysqli_query($db,$sql);
while($row = mysqli_fetch_assoc($result)){
$imageShow = $row['image'];
print $imageShow;
}
?>
So, if your initial page for calling the image is something like:
<body>
<img src="getImage.php?**id=1**" width="175" height="200" />
</body>
Then getImage.php is
<?php
$id = $_GET['id'];
// do some validation here to ensure id is safe
$link = mysql_connect("localhost", "root", "");
mysql_select_db("dvddb");
$sql = "SELECT dvdimage FROM dvd WHERE id=**$id"**;
$result = mysql_query("$sql");
$row = mysql_fetch_assoc($result);
mysql_close($link);
header("Content-type: image/jpeg");
echo $row['dvdimage'];
?>
You're currently passing row, rather than ID.
Your table isn't telling anyone what cell is which, you should be using a primary key as a reference id.
so your SQL should select the table colums and assign them to values within each row, then you pass in the query to pull back the image with a certain primary key.
i.e. row is a PHP value, you need to extract and reference a DB value, a primary key. i.e. ImageID.

Categories