Text = NULL时我该怎么办?

时间:2010-07-06 21:26:45

标签: php marquee

<marquee behavior="alternate" scrolldelay="1" scrollamount="2">
  <?php do { ?>
     <?php echo $row_Recordset1['Name']; ?>:&nbsp;
     <?php echo $row_Recordset1['Text']; ?>&nbsp;
     &#8226;
  <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</marquee>

<?php mysql_free_result($Recordset1); ?>

2 个答案:

答案 0 :(得分:1)

向用户打印友好消息,而不是NULL

<?php echo (NULL === $row_Recordset1['Text']) ? "No value" : $row_Recordset1['Text']; ?>&nbsp;

正如xil3所示,您也可以使用此模式(from the docs):

// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
//       then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
    echo $row["userid"];
    echo $row["fullname"];
    echo $row["userstatus"];
}

答案 1 :(得分:1)

你现在写的方式,$ row_Recordset1在第一次进入循环时将为null。

我已经为你重写了它:

<marquee behavior="alternate" scrolldelay="1" scrollamount="2">
  <?php while($row_Recordset1 = mysql_fetch_assoc($Recordset1)) { ?>
     <?php echo (($row_Recordset1['Name'] != null) ? $row_Recordset1['Name'] : 'n/a'); ?>:&nbsp;
     <?php echo (($row_Recordset1['Text'] != null) ? $row_Recordset1['Text'] : 'n/a'); ?>&nbsp;
     &#8226;
  <?php } ?>
</marquee>

<?php mysql_free_result($Recordset1); ?>