2011-04-26 82 views
0

如果數據庫中的cDeviceRegistrationId列有值,我試圖打印出「1」。下面是代碼:

$結果是一個SQL查詢

while($row = mysql_fetch_assoc($result)) 
    { 
if ($row['cDeviceRegistrationId'] > 0) { 
$a = 1; 
} 

echo "<tr class='forum'>"; 
echo "<td class='forum'>" . $row['intUserID'] . "</td>"; 
echo "<td class='forum'>" . $row['cUsername'] . "</td>"; 
echo "<td class='forum'>" . $row['cEmail'] . "</td>"; 
echo "<td class='forum'>$a</td>"; 
echo "<td class='forum'>" . $row['uCreateDate'] . "</td>"; 
echo "</tr>"; 
} 

回答

1

$a的值,如果不符合條件,這意味着其他迭代可以得到1值錯誤不會被覆蓋。下面是一個修復(更換您的if語句):

$a = ($row['cDeviceRegistrationId'] > 0) ? 1 : ''; 
+0

代替> 0,你怎麼插上,IS NULL? – BigMike 2011-04-26 23:49:13

+0

@BigMike:如果列上的IS NULL爲真,我相信MySQL會返回一個字符串值「1」,所以試試:== == 1「' – 2011-04-27 10:35:31

0

如何:

if(!is_null($row['cDeviceRegistrationId'])){ 
    $a = 1; 
} 
+0

謝謝。這個返回全部爲「1」,即使是不應該的 – BigMike 2011-04-26 23:51:40

相關問題