如何回应<;使用PHP变量作为src属性值的img src
我尝试在PHP中回显图像时出错。这是我的密码:如何回应<;使用PHP变量作为src属性值的img src,php,image,variables,attributes,echo,Php,Image,Variables,Attributes,Echo,我尝试在PHP中回显图像时出错。这是我的密码: $sql = "SELECT id, title, filename FROM images"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { $pathx = "directory/images/";
$sql = "SELECT id, title, filename FROM images";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$pathx = "directory/images/";
echo '
<img src = "'$pathx'.'$row["filename"]'">
';
//filenames in database end with appropriate image extensions like .png
}
$sql=“从图像中选择id、标题、文件名”;
$result=$conn->query($sql);
如果($result->num_rows>0){
//每行的输出数据
而($row=$result->fetch_assoc()){
$pathx=“目录/图像/”;
回声'
';
//数据库中的文件名以适当的图像扩展名结尾,如.png
}
我得到了这个错误:
分析错误:语法错误,意外的“$pathx”(T_变量),应为“,”
或第29行C:\xampp\htdocs\display.php中的“;”
我已经检查了Stackoverflow上类似问题的解决方案,并实现了一些公认的答案,但我的问题仍然没有解决
同时,下面的代码也可以很好地工作。那么为什么上面的代码不能工作呢
echo'<img src = "directory/images/girl-g2109_5_720.jpg">';
echo';
你喜欢这样吗
$pathx = "directory/images/";
$file = $row["filename"];
echo '<img src="'.$pathx.$file.'">';
$pathx=“目录/images/”;
$file=$row[“filename”];
回声';
输出
<img src="directory/images/girl-g2109_5_720.jpg">
<img src="directory/images/girl-g2109_5_720.jpg">
if($result->num\u rows>0){
//每行的输出数据
而($row=$result->fetch_assoc()){
$pathx=“目录/图像/”;
回声';
}
或者也可以使用下面的方式来呈现img标签
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$pathx = "directory/images/"; ?>
<img src = "<?php echo $pathx.$row["filename"] ?>">
<?php } ?>
if($result->num\u rows>0){
//每行的输出数据
而($row=$result->fetch_assoc()){
$pathx=“目录/图像/”;?>
">
看起来数组连接有问题
字符串”和变量$pathx
未正确连接
中间应该有一个连接运算符('.')
将代码更改为以下格式以修复此问题
代码
echo '<img src = "' . $pathx . $row["filename"] . '">';
echo';
输出
<img src="directory/images/girl-g2109_5_720.jpg">
<img src="directory/images/girl-g2109_5_720.jpg">
试试这个
<img src="directory/images/image_name.png">
有多种方式显示图像。您也可以这样使用
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$image=$row["filename"];
?>
<img src="directory/images/<?php echo $image;?>">
<?php
}
?>
if($result->num\u rows>0){
//每行的输出数据
而($row=$result->fetch_assoc()){
$image=$row[“文件名”];
?>
">
输出
<img src="directory/images/image_name.png">
$pathx=“目录/图像/”;$file=$row[“文件名”];echo“”;
echo“”;
Perfect@Abdulla,你解决了我的问题。谢谢我的问题被标记为重复。答案应该是原始的,这是一个非常高级的答案,只有当初学者被特别告知他们的代码有什么问题时才对他们有帮助。原始答案对我来说很有意义,因为我首先从这篇博文中得到了答案(很乐意帮忙:)