0

Hi im new to php and html. im trying to make a page for my first site where one can view all the comments posted on the site. these comments have image attachment. all the info is stored in a mysql database.

ive created a while loop to echo out every comment and image after each other. the text and the username gets updated correctly in each loop cycle but all the images are the same (the last picture loaded). i've been trying to figure it out but can't..

while($query_row = mysql_fetch_assoc($query_run))
{

$username = $query_row['username'];
$comment = $query_row['text'];
$id = $query_row['id'];
$image = $query_row['image'];

$_SESSION['comment_image'] = $image;

echo "Comment by <strong>$username</strong><br>";
echo $comment.'<br>';


echo '<img src=get_image.php><br>';
}

get_image.php:

session_start();

$image = $_SESSION['comment_image'];
header('Content-type: image/jpeg');

echo $image;

output:

Comment by USER1
text1
image3

Comment by USER2
text2
image3

Comment bu USER3
text3
image3

So the problem is that image3 gets echoed out on each comment when it should be image1 then image2 then image3, and so on. the image seems to be the one from the last comment shown.

4

2 回答 2

1

停止使用 blob,将图像文件名\路径存储在数据库中要容易得多.. 但是如果您没有选项..

改变

echo '<img src=get_image.php><br>';

echo '<img src="get_image.php?'.id=$id.'" />';

更改 get_image.php 如下所示:

<?php
$id = (isset($_GET['id']) && is_numeric($_GET['id'])) ? intval($_GET['id']) : 0;
$image = image_from_db($id); // what ever you use to query the db

header('Content-Type: image/jpeg');
echo $image;
?>
于 2013-10-20T19:11:37.327 回答
0

要在输出标签时输出图像,而不是链接到另一个脚本来输出图像,请添加以下函数,并使用图像变量从循环中调用它。

(从这里的答案 - php: recreate and display an image from binary data

function data_uri($contents, $mime) 
{  
    $base64   = base64_encode($contents); 
    return ('data:' . $mime . ';base64,' . $base64);
}

...

echo '<img src="' . data_uri($image,'image/jpeg') .'"><br>';
于 2013-10-20T19:14:41.707 回答