Not able to display image from Oracle Blob using PHP - php

In Oracle DB it shows (BLOB), using the above code to display the image but black screen is displayed.
Blob is stored via Java JDBC connection using setBinaryStream
Code:
<?php
$id = $_GET['serial_id'];
$id1 = $_GET['serial_id1'];
$id2 = $_GET['serial_id2'];
$sql2 = "SELECT * FROM Mapping WHERE PRODUCT ='".$id1."' and (ID='".$id."' or VALUE='".$id2."')";
echo $sql2;
$array2 = oci_parse($conn, $sql2);
oci_execute($array2,OCI_NO_AUTO_COMMIT);
//$result=oci_fetch_array($array2);
$result = oci_fetch_assoc($array2);
if (is_object($result['SCREENSHOT'])) {
$result11 = $result['SCREENSHOT']->load();
header("Content-type: image/JPEG");
echo $result11;
}
?>
Screenshot:

Related

image display from database in php

i want to display image from database and below is my code where empid is the value of textbox, it's showing the image name stored in database but i am not able to echo this picture. anyone can help in this regard.
<?php
include('connect.php');
$result = $db->prepare("SELECT image FROM info WHERE empid= $empid");
$result->bindParam('$empid', $empid);
$result->execute();
for($i=0; $rows = $result->fetch(); $i++){
echo $rows['image'];
}
?>
Change this line.
$result = $db->prepare("SELECT image FROM info WHERE empid= '". $empid ."'");
its only show you the filename that you stored in your db . if you want to show image then first add folder location with image name and pass into the <img src="excatImageLocation">
Example -
$yourExactPath = "YourImageUploadFolderLocation".$rows['image']; // img/yourFilename.jpg;
echo "<img src='$yourExactPath' />
You either concatenate the id onto the text string containing the query or use a parameter place holder and then bind a value to it. Not both, as you were doing.
The most secure way is to use parameters.
<?php
include('connect.php');
$result = $db->prepare("SELECT image FROM info WHERE empid= :empid");
$result->bindParam(':empid', $empid, , PDO::PARAM_INT);
$result->execute();
while ($row = $result->fetch(PDO::FETCH_ASSOC)){
echo $row['image'];
}
?>

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);
?>

Trying to retrieve image from database using phpadmin

This is my php file.
$con=mysql_connect("localhost","root","");
mysql_select_db("food",$con);
$SQL = "SELECT image FROM info WHERE Type='Fruit Pizza' ";
$RESULT = mysql_query("$SQL");
$ROW = mysql_fetch_assoc($RESULT);
header("Content-type: image/png");
echo $ROW['image'];
?>
This is how i add the image into database.
$data = addslashes(file_get_contents('dinner2.png'));
mysql_query("INSERT INTO info(image, Name, Price, type) VALUES('$data','Fruit Pizza','8', 'png')");
Is there anything wrong with my code?? The problem is everytime i try to retrieve the image it show a small grey box instead of the picture.Anyone can help??

How to display an image that is stored in the database?

<?php
$query = "SELECT * FROM table WHERE id = 2";
$result = mysqli_query($con, $query);
$result_set = mysqli_fetch_array($result);
echo $result_set['img'];
?>
Why do shows strange characters, not image?
You need to add Content-Type header at the beginning, something like
<?
header('Content-Type: image/png');
?>
for the PNG 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