我正在尝试在网页上显示图像,其中图像路径存储在数据库中,图像存储在服务器中。但是我无法使用以下代码显示这些图像,所以请有人帮助我解决这个问题,..
<form method="post" enctype="multipart/form-data" action="file_upload.php">
<table>
<?php
$dbhost = 'xxxxxxxx';
$dbuser = 'xxxxxxxxx';
$dbpass = 'xxxxxxxxxx';
$db_name = 'xxxxxxxxxx';
$tbl_name = 'xxxxxxxxxxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("$db_name")or die("cannot select DB");
$query1 = mysql_query("select * from '$tbl_name' where id='1'");
$rows1 = mysql_fetch_array($query1);
$path1 = $rows1['image'];
$query2 = mysql_query("select * from '$tbl_name' where id='2'");
$rows2 = mysql_fetch_array($query2);
$path2 = $rows2['image'];
$query3 = mysql_query("select * from '$tbl_name' where id='3'");
$rows3 = mysql_fetch_array($query3);
$path3 = $rows3['image'];
echo '<tr><td><img src="$path1"></td>' ;
echo '<td><img src="$path2"></td>' ;
echo '<td><img src="$path3"></td></tr>' ;
?>
</form>
</table>
最佳答案
看一下这段代码:
$b = "aaaaaaa";
echo '"$b"'; // will displayt "$b"
echo "'$b'"; // will displayt 'aaaaaaa'
echo "\"$b\"";// will displayt "aaaaaaa"
enter code here
因此,对于您的问题来说,这只是因为如果您希望在字符串中计算变量,则必须将其放在“”而不是“”之间。
<table>
<?php
$path1 = "/upload/image1.png";
$path2 = "/upload/image2.png";
$path3 = "/upload/image3.png";
echo "<tr><td><img src='$path1'></td>" ;
echo "<td><img src='$path2'></td>" ;
echo "<td><img src='$path3'></td></tr>" ;
?>
</table>
或者
<table>
<?php
$path1 = "/upload/image1.png";
$path2 = "/upload/image2.png";
$path3 = "/upload/image3.png";
echo '<tr><td><img src="'.$path1.'"></td>' ;
echo '<td><img src="'.$path2.'"></td>' ;
echo '<td><img src="'.$path3.'"></td></tr>' ;
?>
</table>
阿纳斯
关于php - 无法显示图像路径存储在数据库中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26380579/