试图将表格单元格中的图像链接到sql记录

时间:2013-11-05 01:26:04

标签: php html

我已经在html中创建了一个表格,在每个单元格中我想将其中的图像链接到php中的记录但是我有点困惑如何去做它请帮忙。

<td>
<?php
$con=mysqli_connect("server","username","password","db");
$sql = "SELECT imgurl FROM Workshop_products WHERE id=XXX";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
?>
<img src="<?php echo $row['imgurl'] ?>" />
</td>

尝试向每个单元格添加这样的内容,但它只显示图像。感谢

2 个答案:

答案 0 :(得分:0)

将图片包装成<a> - 标签:

<?php
$link = "user.php?id=$row[id]"; // example: this is the page you want to link to
$img  = $row['imgurl'];
?>

<a href="<?php echo $link; ?>"><img src="<?php echo $img; ?>" /></a>

答案 1 :(得分:0)

我认为您正在尝试显示数据库中显示的所有图像,所以可能是这样:

<?php
$con=mysql_connect("bt2201-1213.bimserver2.com","ihamzadb","xK56K7S6","ihamzadb");
$sql = "SELECT imgurl FROM Workshop_products";
print "<table>";
while($row = mysql_fetch_assoc($sql)){
    print "<td><a href ='" . $row['imgurl'] . "'><img src = '" . $row['imgurl'] . "'></a></td>";
}
print "</table>";