Php图像没有显示

时间:2012-11-25 14:50:28

标签: php mysql

为什么以下php脚本不显示图像。我检查了数据库连接是否正常,Mysql Query也正常。

Php代码:

$upload_directory = "uploaded"; //set upload directory

$getimages = mysql_query("SELECT * FROM property_step3 WHERE propertyid =  
'$propertyid' AND active =1 AND uname = '$uname'"); 

$re2 = mysql_fetch_array($getimages)
$images = mysql_real_escape_string(htmlspecialchars(trim($re2['imgname'])));


echo '<img src="$upload_directory/$images" width="98" height="68"  /></a>'; 

可能是我错了..

我不明白为什么有些人投票给我DOWN ?? !!

更新

<?php
 $uname = $_SESSION['uname'];
 $check = mysql_query("SELECT * FROM property_step1 WHERE uname = '$uname' AND active 
 = 1");
 $num = mysql_num_rows($check);

if($num == 0)
{

echo "<h3>You didn't upload any property. Click <a href='publishadvert.php'>here</a> 
to publish your property</h3>";

}
else
{
    echo "<h3>You have $num published property.</h3>";

    echo "<table width='1020' cellpadding='0' cellspacing='0'>";
        echo "<tr>";
    echo "<td class='tabletr3'><b>Property Title</b></td>";
    echo "<td class='tabletr3'><b>Area</b></td>";
    echo "<td class='tabletr3'><b>State</b></td>";
        echo "<td class='tabletr3'><b>City</b></td>";
    echo "<td class='tabletr3'><b>Country</b></td>";            
    echo "<td class='tabletr3'><b>Images</b></td>";         
    echo "<td class='tabletr3'><b>Action</b></td>";         
    echo "</tr>";

    while($re = mysql_fetch_array($check))
    {
$propertyid = (int) $re['propertyid'];  
$country = mysql_real_escape_string(htmlspecialchars(trim($re['pro_country'])));
$state = mysql_real_escape_string(htmlspecialchars(trim($re['pro_state'])));
$area = mysql_real_escape_string(htmlspecialchars(trim($re['pro_area'])));
$city = mysql_real_escape_string(htmlspecialchars(trim($re['pro_city'])));
$title = mysql_real_escape_string(htmlspecialchars(trim($re['pro_title'])));

 $getimages = mysql_query("SELECT * FROM property_step3 WHERE propertyid = 
 '$propertyid' AND active =1 AND uname = '$uname'");

    $upload_directory = dirname(__file__) . '/uploaded/'; //set upload directory    
while($re2 = mysql_fetch_array($getimages))
{
$images = mysql_real_escape_string(htmlspecialchars(trim($re2['imgname'])));
}



        echo "<tr>";
            echo "<td class='tabletr2'>$title</td>";
            echo "<td class='tabletr2'>$country</td>";
            echo "<td class='tabletr2'>$state</td>";
            echo "<td class='tabletr2'>$city</td>";
            echo "<td class='tabletr2'>$area</td>";     
                  echo '<img src="'.$upload_directory.'/'.$images.'" width="98" 
 height="68"  /></a>';      

echo "<td class='tabletr2'><img src='uploaded/$images' 
/></td>";                               
echo "<td class='tabletr2'><a href='editproperty.php?propertyid=$propertyid&
uname=$uname'>Edit</a> | <a href='deleteproperty.php?propertyid=$propertyid'>Delete</a>
</td>";
    echo "</tr>";
    }  
    echo "</table>";

     }
 ?>

4 个答案:

答案 0 :(得分:1)

错误1 :Waqar Alamgir说的是什么,但更明确。试图在单引号中使用变量。

echo '<img src="$upload_directory/$images" width="98" height="68"  /></a>'; 

将输出

<img src="$upload_directory/$images" width="98" height="68"  /></a>

,而

echo "<img src=\"$upload_directory/$images\" width=\"98\" height=\"68\"  /></a>"; 

echo '<img src="'. $upload_directory . '/' . $images .'" width="98" height="68"  /></a>'; 

echo '<img src="', $upload_directory, '/', $images, '" width="98" height="68"  /></a>'; 

将输出

<img src="uploaded/theimagename.jpg" width="98" height="68"  /></a>

其中$images等于theimagename.jpg

错误2:(Pankaj Khairnar发现)

$re2 = mysql_fetch_array($getimages)

缺少分号。这会打破你的剧本。

$re2 = mysql_fetch_array($getimages);

错误3 (c / o Michael Berkowski)

不要在输出数据上调用mysql_real_escape_string()

答案 1 :(得分:0)

像这样使用

$upload_directory = "uploaded"; //set upload directory
$getimages = mysql_query("SELECT * FROM property_step3 WHERE propertyid =  
'$propertyid' AND active =1 AND uname = '$uname'"); 

$re2 = mysql_fetch_array($getimages);
$images = trim($re2['imgname']);
echo '<img src="'.$upload_directory.'/' .$images. '" width="98" height="68"  /></a>'; 

也;最后在你的陈述中遗漏了

$re2 = mysql_fetch_array($getimages)

应该是这样的

$re2 = mysql_fetch_array($getimages);

答案 2 :(得分:0)

引号出现问题,您无法在单个'

中使用变量
$upload_directory = "uploaded";
$getimages = mysql_query("SELECT imgname FROM property_step3 WHERE propertyid =  
'$propertyid' AND active =1 AND uname = '$uname'"); 
$re2 = mysql_fetch_assoc($getimages)
$images = $re2['imgname'];
echo 'Debug image: ',$images;

echo '<a href="#"><img src="',$upload_directory,'/',$images,'" width="98" height="68"  /></a>'; 

答案 3 :(得分:0)

也许您需要完整的网址来显示图片,例如添加

http://localhost/.../uploaded/50b11aefaad43font-8.jpg

我不确定您计算机上的目录层次结构。

我假设'uploaded'文件夹位于你的htdocs root中。