Trying to retrieve image from database using phpadmin - php

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

Related

Not able to display image from Oracle Blob using 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:

Image from getImage.php won't display in img src

So basically if I go to the getImage link, the image that comes from the database is displayed, but if I use it in a .php file where it will display the image but will resize it to fit the card, it won't show and the alt (which is avatar) shows.
<center>
<img src="getImage.php" class="w3-circle" style="position:absolute; bottom:-20%; left:35.5%; width:30%" alt="Avatar">
</center>
Then the code for the getImage.php:
<?php
session_start();
require './Database.php';
// do some validation here to ensure id is safe
$sql = "SELECT register.FULLNAME, register.IMAGE, gameData.NBA_SCORE FROM register inner join gameData on register.ID = gameData.ID WHERE NBA_SCORE = (select max(NEW_SCORE) from gameData)";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($result);
echo "<img src='".$row['IMAGE']."'>";
?>
Anything wrong with what I am doing?
Problem
HTML trys to get an valid image file like jpg or png but gets an php/text file with the content <img src="...">.
Solution
You need to change the php files content type to an image and output the image files data:
header("Content-Type: image/jpeg");//or image/png
echo file_get_contents("$imagepath");//file path not url!!!
Code
<?php
session_start();
require './Database.php';
header("Content-Type: image/jpeg");
// do some validation here to ensure id is safe
$sql = "SELECT register.FULLNAME, register.IMAGE, gameData.NBA_SCORE FROM register inner join gameData on register.ID = gameData.ID WHERE NBA_SCORE = (select max(NEW_SCORE) from gameData)";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($result);
$path = $row['IMAGE'];//Maybe you need to change this if you only save an url in the database
echo file_get_contents($path);
?>

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

I am using Chrome and cant get image to show from mysql using php

Firstly I know I shouldn't put the image in a DB but I am still learning.
I want to show the image, where am I going wrong?
In my database I have a column Image that is a BLOB. Small sized image. And I've checked an image array is there.
After I connect the db here is the php
$result = mysql_query("SELECT Image FROM table WHERE ID = 1");
$row = mysql_fetch_array($result);
echo $row["image"];
Any help appreciated.
All I want is just the image to show. Nothing fancy as I will expand on it once I get an image up on the screen.
I think I need an extra step between fetch array to split a variable that contains just the image and to display that. This is where I get lost.
Cheers.
This is the code I use to update the DB (from a form)
$myphoto = $_FILES['MyPhoto'];
$query = mysql_query("UPDATE table SET Image = '$myphoto' WHERE ID = 1") or die(mysql_error());
You need to echo an HTML <image> tag with the src set to your query results to render an image. Use this code:
$result = mysql_query("SELECT Image FROM TABLE WHERE ID = 1");
$row = mysql_fetch_array($result);
echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['Image'] ).'"/>';

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

Categories