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']."\">";
Related
I wanted to retrieve machine image pass by id. But issue is that i would be getting all machine images instead of particular one. So please guide me what's wrong i have done.
<?php
$query1=mysqli_query($con,"select * from photo where m_id = $m_id");
while($row=mysqli_fetch_array($query1)){
?>
<img src="<?php echo $row['location']; ?>" height="150px;" width="150px;">
<?php
}
?>
You should filter data using WHERE to fetch only certain rows.
Just Add:
$m_id = $_POST['m_id'];
$query1=mysqli_query($con,"select * from photo where m_id = $m_id ");
hope this help
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 having a little trouble trying to display the image at ID: 2 in my store table...
here's what i've come up with (p.s. the reason I set $id = 2 is because I plan on making this a loop where I will increment that value to print other images)
<!DOCTYPE html>
<html>
<head>
<title>Gallery</title>
</head>
<body>
<?php
mysql_connect ...
mysql_select_db ...
$id = 2;
$image = mysql_query("SELECT * FROM store WHERE id=$id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
echo $image;
?>
</body>
</html>
First, some caveats:
Don't use the mysql_* functions
Don't store images in SQL. Store the location. (You may already be doing this).
If you're storing the location, you'll need to add <img> tags to the echo
echo "<img src='$image'>";
Maybe you need to echo the markup also?
echo '<img src="'.$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 will get right into the chase! i got the following code to display an image and some data stored in a table
<?php
if (isset($_GET['p'])){ //onclick i call that script with parameter
$d=$_GET['p']; //so p is the parametered passed to the script
$connect=mysql_connect("localhost","user","")
or die ("Check your server connection");
$db_found = mysql_select_db("mydb", $connect);
$SQL = "SELECT * FROM table
WHERE d ='$d'";
$result = mysql_query($SQL);
$row=mysql_fetch_array($result);
echo "DATA: $row[data]<br>";
header('Content-type:image/jpg');
$content = $row['image'];
echo $content;
}
?>
i get the name of the string.if i remove all the echo and leave only the echo $content; my image is shown.Furthermore if remove echo$content and leave all other echoes,my echoes is shown again!! why cant i have both image and echos together?what should i do ? thnx in advance!
You have to call headers before other outputs (for example echo). Also you should use image/jpeg . Try this :
header('Content-type:image/jpeg');
echo "DATA: $row[data]<br>";
$content = $row['image'];
echo $content;
Correct mime-tag for jpg images is "image/jpeg" - try that instead
As the other answer points out you need to have all your echos after the header, but if you have both echos the image probably won't display probably, you just need the one echo (echo $content) and the one header.