在 img src 标签中包含一个 PHP 结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9654581/
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
include a PHP result in img src tag
提问by Jjames
I am trying to do something I know is probably simple, but I am having the worst time. I have functioning so far: 1.Script to upload image files to server 2. write the image file names to the database 3. I want to retrieve the image filename from the db and add it to the img src tag here is my retrieval script
我正在尝试做一些我知道可能很简单的事情,但我正在度过最糟糕的时光。到目前为止我已经运行了: 1. 将图像文件上传到服务器的脚本 2. 将图像文件名写入数据库 3. 我想从数据库中检索图像文件名并将其添加到 img src 标签这里是我的检索脚本
<?php
$hote = 'localhost';
$base = 'dbasename';
$user = 'username';
$pass = '******';
$cnx = mysql_connect ($hote, $user, $pass) or die(mysql_error ());
$ret = mysql_select_db ($base) or die (mysql_error ());
$image_id = mysql_real_escape_string($_GET['ID']);
$sql = "SELECT image FROM image_upload WHERE ID ='$image_id'";
$result = mysql_query($sql);
$image = mysql_result($result, 0);
header('Content-Type: text/html');
echo '<img src="' $image'"/>';
?>
I was trying to pass the Value through image.php?ID=2 but no luck
我试图通过 image.php?ID=2 传递值但没有运气
The PHP script successfully returns the filename, but I cannot for the life of me get it to print it to the html
PHP 脚本成功返回文件名,但我终生无法将其打印到 html
Any suggestions, please and thank you very much :)
任何建议,请并非常感谢您:)
OK, it does return the proper tag, but now it seems as though the script doesnt run to generate the tag. I have tried two ways:
好的,它确实返回了正确的标签,但现在似乎脚本没有运行来生成标签。我尝试了两种方法:
<div class="slides">
<div class="slide">
<div class="image-holder">
<?php
include ("image.php?ID=2");
?>
</div>
and:
和:
<img src="image.php?ID=2" alt="" />
but neither one will insert the filename... I need to identify each img src by the primary key, so I was passing it the ID from each image src location but alas, my PHP ninja skills need to be honed.
但是没有人会插入文件名...我需要通过主键识别每个 img src,所以我将每个图像 src 位置的 ID 传递给它,但唉,我的 PHP 忍者技能需要磨练。
Just to clarify: I am uploading images to the server, recording the filenames in a DB and calling that filename in an HTML doc...there are several in each one so I need to pass the ID (i.e. 1,2,3 ) to correspond to the primary key in the table. But I cant get the script to process the tag first. If I go to view source, I can click the script and get the proper result...
只是为了澄清:我正在将图像上传到服务器,在数据库中记录文件名并在 HTML 文档中调用该文件名......每个文件中有几个所以我需要传递 ID(即 1,2,3 )对应表中的主键。但是我无法让脚本先处理标签。如果我去查看源代码,我可以单击脚本并获得正确的结果...
Thanks again, you guys and girls are very helpful
再次感谢你们,你们非常乐于助人
回答by maxedison
You're missing the concatenation operator: .
:
您缺少连接运算符.
::
echo '<img src="' . $image . '"/>';
回答by Adam
You can do it as you did but you had the single quotes in twice, (unless you were meaning to use concatenation - which is unnecessary - if you want this see the other answer).
你可以像你一样做,但你有两次单引号,(除非你打算使用连接 - 这是不必要的 - 如果你想要这个,请参阅另一个答案)。
echo "<img src=\"$image\"/>";
Or the longer form with braces if you need to embed inside text.
如果您需要嵌入文本中,或者使用带大括号的较长形式。
echo "<img src=\"${image}\"/>";
I'd recommend using heredocsyntax for this if you're doing lots of HTML. This avoids the need to have lots of echo lines.
如果您正在执行大量 HTML,我建议为此使用heredoc语法。这避免了需要有很多回声线。
echo <<< EOF
<div class="example">
<img src="$image" />
</div>
EOF;
回答by mumush
Try using this syntax:
尝试使用以下语法:
echo "<img src=\"$imagePath\" />";
It works with double quotes provided you escape the quotes in the src attribute. Still not sure why the singles don't work.
如果您对 src 属性中的引号进行转义,则它适用于双引号。仍然不知道为什么单打不工作。
回答by Your Common Sense
there are 2 major flaws with your design
你的设计有两个主要缺陷
- There is no
image.php?ID=2
file on your disk. - There is absolutely no point in including image.php file. You have to get the name right in the file you are working with. don't you have this row already selected from the database? Why not just print the image name then?
- And yes, you are using single quotes where double ones needed.
image.php?ID=2
您的磁盘上没有文件。- 包含 image.php 文件绝对没有意义。您必须在您正在使用的文件中获得正确的名称。你不是已经从数据库中选择了这一行吗?为什么不直接打印图像名称呢?
- 是的,您在需要双引号的地方使用单引号。