2016-05-01 120 views
0

所以我做了配置文件类型的事情,与状态更新,但是,当没有状态的数据库中,当然显示空白,我试图使它如此,如果数据库是empty,然后echo "You have no Feed"这里是我的代码:如果数据库没有记录,则显示回显?

<?php 
$status = mysql_query("SELECT * FROM status WHERE user='$username' ORDER BY date DESC"); 
while($row = mysql_fetch_array($status)) 

if($row['user'] == '') { echo "You have no Feed"; } else { 

{ 

echo "<table width='520'>"; 
echo "<tr>"; 
echo "<td width='53' height='57' valign='top' rowspan='3'><img src='../includes/images/profile/$image' height='50px' width='50px' style='border: 1px solid #000'></td>"; 
echo "<td width='411' valign='top'><font color='#CCCCCC'>". $row['sentby'] . "</font><br><font size='1px' color='#CCCCCC'>". $row['date'] . "</font></td>"; 
echo "</tr>"; 
echo "<tr>"; 
echo "<td></td>"; 
echo "</tr>"; 
echo "<tr>"; 
echo "<td>"; 
echo "</td>"; 
echo "</tr>"; 
echo "<tr>"; 
echo "<td colspan='2' valign='top'><font color='#CCCCCC'>". $row['status'] . "</font></td>"; 
echo "</tr>"; 
echo "</table>"; 
echo "</p>"; 

} 
} 

?> 

的方式我的个人资料的作品,是每个人都有自己的空间,如果你去别人的个人资料,并在他们的“墙”评论ofcourse,只会留在他们的墙上,我的数据库创建为'id' 'status' 'user' 'sentby''user' (being the username of who's wall it was posted on)'sentby' (to display who sent it)

基本上,我需要'user'列匹配if无法找到用户名,如果找不到用户名,请回复邮件。

+0

和'$ row ['user']'是从哪里来的?请记住,如果您的默认列值为NULL,那么它不等于空。但是贴出来的话,你似乎把马车放在马前,如果那是你的完整代码。 –

+0

'$ row ['user']'来自它下面的mysql_query。 –

+0

'!$ row ['user']'< - 不要按照这个。 –

回答

1

mysql_num_rows函数将返回查询获取的行数。这应该工作。

 $status = mysql_query("SELECT * FROM status WHERE user='$username' ORDER BY date DESC"); 


    if(mysql_num_rows($status) > 0){ 
     while($row = mysql_fetch_array($status)) 

     echo "<table width='520'>"; 
     echo "<tr>"; 
     echo "<td width='53' height='57' valign='top' rowspan='3'><img src='../includes/images/profile/$image' height='50px' width='50px' style='border: 1px solid #000'></td>"; 
     echo "<td width='411' valign='top'><font color='#CCCCCC'>". $row['sentby'] . "</font><br><font size='1px' color='#CCCCCC'>". $row['date'] . "</font></td>"; 
     echo "</tr>"; 
     echo "<tr>"; 
     echo "<td></td>"; 
     echo "</tr>"; 
     echo "<tr>"; 
     echo "<td>"; 
     echo "</td>"; 
     echo "</tr>"; 
     echo "<tr>"; 
     echo "<td colspan='2' valign='top'><font color='#CCCCCC'>". $row['status'] . "</font></td>"; 
     echo "</tr>"; 
     echo "</table>"; 
     echo "</p>"; 
     } 
     } else { 
     echo "<p>This user has not yet updated their status.</p>"; 
     } 
+0

mysql_query不安全,很快就会被弃用,使用ORM或使用PDO,mysqli – datelligence

+2

@datelligence它*已被*弃用和*删除*在PHP 7中。 – Panda

+0

是的,但是这只是一小群朋友,但是,我将学习如何使用PDO和Mysqli,我只想首先在网上找到它,然后我会重新编码它 –

相关问题