简体   繁体   中英

How to use the result of mysql_fetch_array?

I'm using a while statement on this and I can echo each row fine ie

echo $row['myrow'];

but what I want is to have the result put into a link like so:

echo "<img src='http://www.mysite.com/images/$row['myrow'].jpg'>";

But it doesn't work. What am I doing wrong?

Either echo it this way:

echo "<img src='http://www.mysite.com/images/{$row['myrow']}.jpg'>";

Or, IMHO much better, this way:

echo "<img src='http://www.mysite.com/images/".$row['myrow'].".jpg'>";

Give the documentation on double quoted-strings a quick refresh.

Another nice way to do it is to only use PHP for the dynamic part of the code. I think it results in nicer looking code.

<img src="http://www.mysite.com/images/<?php echo $row['myrow']; ?>.jpg">

Then of course the whole img tag should not be in a PHP code block, since it regular HTML.

您已将网站网址写在example.com上

echo "<img src='example.com/images/'.$row['myrow'].'.jpg'>";

You need to take care of your quotes... Try this:

echo '<img src="http://www.mysite.com/images/'.$row['myrow'].'.jpg" />'; 

Also notice that you didn't close the element.

访问字符串中的数组元素和对象属性/方法必须用大括号括起来( 字符串解析

echo "<img src='http://www.mysite.com/images/{$row['myrow']}.jpg'>";

使其完整,大声笑

echo "<img src='http://www.mysite.com/images/$row[myrow].jpg'>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM