9

这是我从表中回显的描述变量:

$description = mysql_result($result,$i,"description");

有时第 $i 条记录是空的,并且其中没有任何数据/没有描述。

我想要做的是为空的记录回显“没有可用的描述”

if (isset($description)){ echo "No description available";}
else{ echo $desctipion;}

我的尝试不起作用,因为它随后回显 No description available for each record 即使是那些不为空的记录。

答案是什么?

4

3 回答 3

27

isset($description)结果为真,因为 $description 仍然设置,即使它的值为“空”。你需要使用的是的。

if (empty($description)) {
    echo "No description available";
} else {
    echo $description;
}
于 2009-10-19T23:05:56.323 回答
2

这取决于你的意思。

mysql_result在失败时返回FALSE,如果您指定了无效(行、字段),就会发生这种情况。要检查这一点,您需要使用相同性运算符===,它会检查值和类型。相等运算符==empty()函数和 if 语句的条件评估都检查值但不检查类型。

这意味着使用其中一种方法,所有等同于 的各种值之间没有区别Boolean FALSE,例如空字符串、空数组、字符串'0'和值NULL

因此,如果您想真正彻底地了解它,您可以执行以下操作:

if ($description === FALSE) {
  throw new Exception("The row $i was out of range in query $query.");
} else if ($description === NULL) {
  // assuming that the description field has a default value of NULL
  // * this one I'm not sure about.. the documentation for mysql_result claims
  //   that it returns a string, so this may never happen.
  //   It's left as an exercise for the reader.
  throw new Exception("Uninitialized value in result row $i of db query $query");
} else if ($description === '') {
  echo "No description available";
} else {
  echo $description;
}

由于在一组类似的条件下empty()返回与 的等式 ( ) ,因此在结果可能实际为 的情况下,在类型检查中更严格的这一点尤其重要。 true==FALSE"0"

显然我不允许发布多个超链接,因此我无法链接到比较运算符的文档(“ http://php.net/manual/en/language.operators.comparison.php ”)或空函数(“ http://php.net/empty ”)。幸运的是,他们的安全性相对宽松。呜呜!

于 2009-10-20T04:33:07.300 回答
0

使用mysql_affected_rows功能:

$qtd = mysql_affected_rows($result);
于 2009-10-19T23:04:43.157 回答