使用php变量显示图像

时间:2022-01-24 21:22:06

I'm trying to display images by taking their file path from an sql table, but i'm having a lot of troubles.

我试图通过从sql表中获取他们的文件路径来显示图像,但是我遇到了很多麻烦。

Here is whats going on:

这里有个问题:

$image is a variable containing the text "itemimg/hyuna.png" which is path to an image.

$image是一个包含“itemimg/hyuna”文本的变量。png是图像的路径。

$image = 'itemimg/hyuna.png';

I assumed I would be able to display the image outside of the php block like so:

我假设我可以在php块外面显示图像,如下所示:

<img src= "<? $image ?>" alt="test"/>

This doesn't work though for some reason.

但出于某种原因,这行不通。

So I thought maybe it's not able to read the variable outside the php block(i'm a beginner), so for testing i did:

所以我认为它可能无法在php块之外读取变量(我是初学者),所以为了测试我做了:

<h1> "<? $image ?>" </h1>

It displays itemimg/hyuna.png as a h1 banner.

它显示itemimg / hyuna。作为h1的横幅。

Meaning it's accessing the varible fine.

这意味着它可以很好地访问变量。

So I thought maybe the path is wrong. So I tried:

所以我认为这条路可能是错的。所以我试着:

<img src= "itemimg/hyuna.png" alt="test"/>

This displays the image perfectly.

这将完美地显示图像。

So now I'm stuck scratching my head why the first bit of code displays nothing but the text "test" from "alt="

所以现在我一直在挠头为什么第一段代码只显示了alt=的文本"test"

Extra question: How do I go about assigning a value from an sql cell to a variable? I attempted the following with no luck:

额外问题:如何将sql单元格中的值分配给变量?我不走运地尝试了以下方法:

$q = "select * from item where id=$id";
$results = mysql_query($q);
$row = mysql_fetch_array($results, MYSQL_ASSOC);
$image = ".$row['image'].";

item is a table with a collumn: image which contains file paths to images

项目是一个包含collumn: image的表,其中包含到image的文件路径

4 个解决方案

#1


6  

First of all, you should not use PHP Shorttags.

首先,您不应该使用PHP Shorttags。

When you use the PHP Shorttags you have to say:

当你使用PHP时,你必须说:

<img src="<?=$image ?>" alt="test" />

But i would encourage to escape the Content off the variable like this:

但是我鼓励大家把内容从变量中去掉

<img src="<?php echo htmlspecialchars($image); ?>" alt="test" />

Your extra question:

This should lead to an syntax error because the string could not be parsed, just use $image = $row['image'];

这将导致语法错误,因为字符串无法解析,只需使用$image = $row['image'];

#2


3  

Try this

试试这个

<img src= "<?php echo $image ?>" alt="test"/>

< img src = " < ?php echo $image ?>" alt="test"/>

#3


2  

try

试一试

<img src= "<?= $image ?>" alt="test"/>

or

<img src= "<? echo $image; ?>" alt="test"/>

#4


-1  

try this

试试这个

<img src= "<?php echo $image ?>" alt="test"/>

#1


6  

First of all, you should not use PHP Shorttags.

首先,您不应该使用PHP Shorttags。

When you use the PHP Shorttags you have to say:

当你使用PHP时,你必须说:

<img src="<?=$image ?>" alt="test" />

But i would encourage to escape the Content off the variable like this:

但是我鼓励大家把内容从变量中去掉

<img src="<?php echo htmlspecialchars($image); ?>" alt="test" />

Your extra question:

This should lead to an syntax error because the string could not be parsed, just use $image = $row['image'];

这将导致语法错误,因为字符串无法解析,只需使用$image = $row['image'];

#2


3  

Try this

试试这个

<img src= "<?php echo $image ?>" alt="test"/>

< img src = " < ?php echo $image ?>" alt="test"/>

#3


2  

try

试一试

<img src= "<?= $image ?>" alt="test"/>

or

<img src= "<? echo $image; ?>" alt="test"/>

#4


-1  

try this

试试这个

<img src= "<?php echo $image ?>" alt="test"/>