php 使用php显示图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13662996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
display image using php
提问by Ross
I have a the following code that i intend to should return a filepath name from mysql in the php section and then display the image in html. But i am only getting up a tiny thumbnail in my browser. By the way "username" and "imagefile" are the only columns in the table "images".
我有以下代码,我打算在 php 部分从 mysql 返回一个文件路径名,然后在 html 中显示图像。但我只是在我的浏览器中出现了一个小缩略图。顺便说一下,“用户名”和“图像文件”是“图像”表中唯一的列。
I'm sure there is just some silly mistake but i need a fresh pair of eyes to spot it. Thanks a lot in advance. P.S i know i should really me moving over to mysqli but i will simply translate at a later date. Cheers
我确信这只是一些愚蠢的错误,但我需要一双新的眼睛来发现它。非常感谢。PS我知道我真的应该转移到mysqli,但我会在以后简单地翻译。干杯
<?php
session_start();
$username = $_SESSION['username'];
$con = mysql_connect('localhost','root','password');
mysql_select_db("db");
$profileimage = mysql_query("
SELECT * FROM images WHERE username='$username'
");
$row = mysql_fetch_array($profileimage);
$showimage = $row['imagefile'];
?>
<html>
<img src = "$showimage">
</html>
回答by rg88
First off, HTML doesn't know what "$showimage"means. That is a PHP variable and HTML cannot interpret it. You need to output it so that HTML can just deal with the result.
首先,HTML 不知道什么"$showimage"意思。这是一个 PHP 变量,HTML 无法解释它。您需要输出它,以便 HTML 可以处理结果。
So if the value for $showimageis "/images/foo.jpg"you would need something like:
因此,如果值为$showimage是,"/images/foo.jpg"您将需要以下内容:
<img src="<?php echo $showimage; ?>" />
which would give you
这会给你
<img src="/images/foo.jpg" />
Now, switching things to mysqli is as simple as replacing mysqlwith mysqli. It's no more complicated than that. Since it looks like you are just starting to learn about these things you may as well, when you go to improve things, learn about PDO.
现在,交换东西的mysqli很简单,只要更换mysql用mysqli。没有比这更复杂的了。既然看起来你刚刚开始学习这些东西,你也可以,当你去改进的时候,学习 PDO。
回答by koopajah
Is this your current real code or is it a simplified version? If it is your real code the problem is in the HTML part where the PHP variable is unknown, you should do this:
这是您当前的真实代码还是简化版本?如果是您的真实代码,问题出在 PHP 变量未知的 HTML 部分,您应该这样做:
<html>
<img src ="<?php echo $showimage; ?>" />
</html>
回答by saurabh
<?php
$db = mysqli_connect("localhost:3306","root","","databasename");
$sql = "SELECT * FROM table_name ";
$sth = $db->query($sql);
while($result=mysqli_fetch_array($sth)){
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'" height="100" width="100"/>';
}
?>
Works for me
为我工作

