如何在使用这个时查找和替换空行.....
$Qemaster="select * from emaster where `branch`='$bid' and `department`='$did' and `status`!='L'";
$Remaster=mysql_query($Qemaster);
while($Rowemaster=mysql_fetch_array($Remaster)){
$empcode=$Rowemaster[id];
$name=$Rowemaster[name];
$Tleave=0;
echo "<tr>";
echo "<td rowspan='2'>".$name."</td>";
echo "<td>Leave</td>";
$Qlp="select * from lpsummary where ((`month` IN(04,05,06,07,08,09,10,11,12) and `year`='$year') or (`month` IN (01,02,03) and `year`='$Nyear')) and `empcode`='$empcode'";
$Rlp=mysql_query($Qlp);
$RRlp=mysql_num_rows($Rlp);
while($Rowlp=mysql_fetch_array($Rlp)){
$leave=$Rowlp['leave'];
$Tleave=$Tleave+$leave;
if($leave==NULL){
$Eleave='-';
}
else{
$Eleave=$leave;
}
echo "<td>".$Eleave."</td>";
}
echo "<td><font color='red'>".$Tleave."</font></td>";
echo "<td><font color='green'>".substr(($Tleave/$RRlp),0,4)."</font></td>";
echo "<tr><td>Percentage</td>";
}
如果有空行...我想将其替换为 - 而不是 echo "".$Eleave."";
最佳答案
实际上你可以通过查询直接做到这一点,所以你的 php 代码中不会有任何 IF-ELSE
条件。通过使用 COALESCE()
,例如
此外,养成指定列名的习惯,而不是使用 *
。
SELECT colnames,..., COALESCE(`leave`, '-') `Leave`
FROM lpsummary
WHERE .....
更新 1
SELECT COALESCE(l.leave, '-') AS `LEAVE`
FROM lpsummary l
WHERE (
(month IN(04,05,06,07,08,09,10,11,12) and year='$year') or
(month IN (01,02,03) and year='$Nyear')
) and empcode='$empcode'
作为旁注,查询易受 SQL Injection
攻击如果变量的值(s) 来自外部。请查看下面的文章,了解如何预防它。通过使用 PreparedStatements
,您可以摆脱在值周围使用单引号。
关于php - 替换 MySQL 中的 NULL 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16015143/