使用php显示图像 [英] display image using php
问题描述
我有以下代码,我打算在php部分中从mysql返回文件路径名称,然后以html显示图像.但是我在浏览器中仅获得一个很小的缩略图.顺便说一下,用户名"和图像文件"是表图像"中的唯一列.
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".
我敢肯定,这只是一个愚蠢的错误,但是我需要一双崭新的眼睛才能发现它.在此先多谢.附言:我知道我真的应该转而使用mysqli,但是稍后我会翻译.干杯
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
<?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>
推荐答案
首先,HTML不知道"$ showimage"
的含义.那是一个PHP变量,HTML无法解释它.您需要输出它,以便HTML可以处理结果.
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.
因此,如果 $ showimage
的值为"/images/foo.jpg"
,则需要类似以下内容:
So if the value for $showimage
is "/images/foo.jpg"
you would need something like:
<img src="<?php echo $showimage; ?>" />
这会给你
<img src="/images/foo.jpg" />
现在,将事情切换到mysqli就像用 mysqli
替换 mysql
一样简单.没有比这更复杂的了.既然您似乎才刚刚开始学习这些知识,那么,当您进行改进时,请学习有关PDO的知识.
Now, switching things to mysqli is as simple as replacing mysql
with 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.
这篇关于使用php显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!