PHP if else语句数据库记录为空它将显示默认图片 [英] PHP If else statement that a database record is empty it will display a default picture
问题描述
嗨基本上我正在尝试创建一个简单的IF语句,如果没有输入到我的数据库中,则显示默认图片。我使用服务器存储我的图片和数据库来存储文件名,所以我可以显示图像,如果它在数据库中有文件名但我想要if语句说明记录是否为空显示此默认值图片。我在这里有一些代码我已经尝试过但它没有任何想法吗?我尝试了其他一些方法,但它们也没有用。
Hi basicly I am trying to create a simple IF statement that shows a default picture if one hasn't been entered in to my database. I am using server to store my picture and a database to store the file name, so I can get the image to display if it has a file name in the db but I want the If statement to say if the record is empty display this default image. I have some code here that I have tried however it doesn't work any thoughts? I tried a few other ways of doing it but they didn't work either.
干杯。
代码到目前为止:
//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM db*****") or die(mysql_error());
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{
?>
<div class="member">
<div class="imageSection">
<?
if($info['photo'] == '')
{echo "<img class=\"memberImage\" src=images/default.jpg>";}
else {echo "<img class=\"memberImage\" src=images/".$info['photo'] .">";}
?>
</div>
<div class="memberInfo">
<? Echo "<p><strong>Name: ".$info['nameMember'] . "</strong></p>";
Echo "<p>Position: ".$info['bandMember'] . " </p>";
Echo "<p>About Band Member ".$info['nameMember'] .":".$info['aboutMember'] . "</p>";
Echo "<p>Other Bands: ".$info['otherBands'] . " </p><br/></div></div><br class=\"clearBoth\"/>";
}
?>
推荐答案
如何简单的三元:
$photo = ($info['photo'] == null) ? "default.jpg" : $info['photo'];
echo "<img class=\"memberImage\" src=images/". $photo .">";
这篇关于PHP if else语句数据库记录为空它将显示默认图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!