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.
Related
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);
?>
Here is the simple code what i tried.
I am not able to display the image presented in database
Please help me in this regard.
I am using two files
1) db.php
This is for connecting mysql database and getting a record.
Images will be saved in a jp2 format.
2) test.php
Displaying record using php.
**If images are saved in JP2 format then then how can i convert these images and display into a web page.
If iam using this code some junk(bytecode say) is displaying ian a web page**
This is my db.php
<?php
function getResidentPhoto() {
$dbcnx = #mysql_connect('127.0.0.1', 'root', 'root#19211');
if (!$dbcnx) {
die('<p>Unable to connect to the ' . 'database server at this time.</p>'
);
}
if (!#mysql_select_db('finaldb')) {
die('<p>Unable to locate the ' . 'database at this time.</p>');
}
$selectrc = 'select BPFT_Photo from photo_templates limit 1';
$result = #mysql_query($selectrc, $dbcnx);
if (!$result) {
die('<p>Error performing query: ' . mysql_error() . '</p>');
} else {
}
$n = mysql_num_rows($result);
$row = mysql_fetch_assoc($result);
if ($n > 0) {
$Photo_Details = $row['BPFT_Photo'];
return $Photo_Details;
}
}
?>
and my test.php is
<?php
echo' <html>';
echo'<body>';
echo' <table>';
echo' <tr><td>';
$residentPhoto = getResidentPhoto();
$image_type_to_mime_type0 =image_type_to_mime_type($residentPhoto)
header('Content-type:$image_type_to_mime_type0');
echo' '.$residentPhoto;
echo'</td>';
echo'</tr>';
echo'</table>';
echo'</body>';
echo'</html>';
?>
Judging by the content-type header, I'm going to guess that $residentPhoto is raw image data. If that is the case, then there should be absolutely no output other than the image itself.
In test.php you should include or require the db.php file.
make sure the path of image is given and make sure it is correct.
You're mixing HTML with the actual image.
You just have to output the image in a separate file, which you have to include in your html with an "img" tag.
For example:
<img src="image.php?imageId=1" alt="Image" />
There is another solution but it has some drawbacks:
You can embedd image data in your HTML by using Data URLs: http://davidwalsh.name/data-uri-php but this will increase your HTML code size and (maybe) you will get problems with older browsers (IE < 8).
So I've got some rows in a table that I want to use to concatenate. I also have a file that runs a query to count comments. I want to pull that number from that file.
I was doing it with straight html but my website is getting too big so I'm trying to make it dynamic with php and mysql.
So with my Html its hard coded and works:
<?php include_once("comments/post_title/tpost_title.php") ?>
So I thought I could just concatenate it to be the same, but for some reason my web browser (chrome) thinks its a comment.
Php:
$query = "SELECT post_title FROM sessions";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
{
echo "<a href=\"#" .strip_tags("{$row['post_title']}"). "\">read comment";
echo "<?php include_once(\"comments/" .strip_tags("{$row['post_title']}")."/t". strip_tags("{$row['post_title']}"). ".php\") ?>";
echo "</a>";
}
I've read about "#file_get_contents" but my understanding of it is limited. As I understand it has to be set to a variable and then I would have to insert that into my while loop. But I get lost in how the variable changes to the next row in my table to pull the next post_title.
I guess another option would be to put the query that's in that file that counts comments into this loop, but then I'd have to put a variable into the query. (Is that "good" coding?) Say for instance:
$query = "SELECT post_title FROM sessions WHERE session = 'variable';
Thanks in advance for the help and insight.
To read the file in the loop, you need:
while($row = mysql_fetch_assoc($result))
{
echo "<a href=\"#" .strip_tags("{$row['post_title']}"). "\">read comment";
include_once("comments/" .strip_tags("{$row['post_title']}")."/t". strip_tags("{$row['post_title']}"). ".php");
echo "</a>";
}
If the file you read isn't PHP but straight HTML, then you can use readfile() instead of include(). The syntax is the same.
To be sure, though, it would be better something like,
$file = "comments/" .strip_tags("{$row['post_title']}")."/t". strip_tags("{$row['post_title']}"). ".php";
if (file_exists($file))
readfile($file);
else
echo "ERROR";
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 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']."\">";