<?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
Related
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:
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);
?>
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);
?>
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??
I'm trying to develop the dynamic site using PHP and obsolete mysql_query.
I need to extract limited contents from my database so I've used substr and this content is placed at the index page. But while the user edits the page, the content is formatted, sometimes the font size is too large, sometimes the content has lots of <br>. It destroys the look of my website. I'm troubled by unnecessary <br> and want to remove it and also want to ignore the formatted font size in the index page.
My code goes like this:
<?php
$sql = "SELECT contents FROM table";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$contents = $row['contents'];
echo substr($fld_page_details, 0,125);
}
?>
Try the following:
<?php
$sql = "SELECT contents FROM table";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$contents = $row['contents'];
echo str_replace('<br>', '', $contents);
}
?>