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);
?>
Related
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'];
}
?>
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??
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 can i use php to get an image url from database (based on user id) and display it out as an image.
http://mysite.com/img.php?id=338576
The code below shows a php script goes to a database , check whether the specific user(using the id in the link above) exist then get the image url out of that user's row.
<?php
//connect to database
include ("connect.php");
//Get the values
$id = mysql_real_escape_string($_GET['id']);
//get the image url
$result = mysql_query("SELECT * FROM users WHERE id='$id'")
or die(mysql_error());
$row = mysql_fetch_array($result);
if($row)
{
$img_url = row['imgurl'];
mysql_close($connect);
}
else{
}
?>
The $Img_url is an actual image link www.asite.com/image.jpg
The problem is how can i use php to make an image from the image link($img_url)? By which http://mysite.com/img.php?id=338576 will turn into an image.
I hope someone could guide me
Thanks!
The easiest way:
header('Location: url/to/image');
You could also proxy the request, which uses your bandwidth:
echo(file_get_contents('url/to/image'));
This is pretty basic stuff. Am I understanding you correctly? I would just do this:
<?php
$img_html = '<img src="' . $img_url . '" />';
echo $img_html;
?>
Or check this answer:
How do I script a php file to display an image like <img src="/img.php?imageID=32" />?
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