如果一行不为null且不为空,我该如何显示它


How do I display a row if it is not null and not empty?

我有一个页面,显示mysql_query中的各种字段。我只想在值可用时显示以下字段/行。

$web = $row['Website'];

<?php 
if ($web = NULL)
{?>
<!-- DO NOTHING -->
<?php
}
else
{?>
<tr>
<td valign='top' colspan='3' align='center'><DIV CLASS='standings_title'><?php echo $web ?></div></td>
</tr>
<?php
}
?>

希望有人能告诉我我做错了什么。

<?php if($web):?>
<tr>
<td valign='top' colspan='3' align='center'><DIV CLASS='standings_title'><?php echo $web ?></div></td>
</tr>
<?php endif; ?>
if (isset($value) && strlen($value) > 0) {
  //stuff
}

这比使用空($value)或仅使用if($value)要好,因为php认为"0"之类的东西是空的。但是,如果您可以保证值永远不会是"0",那么它们在功能上是等效的,并且!空($value)会更快。

来自php手册:

The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)

使用isset()

if(isset($web))
{
}

if($web)
{
}
$web = $row['Website'];
// Trim whitespace so a single blank space doesn't display as a populated value.
$web = trim($web);
if (!empty($web) {
  // Display it.
}

如果行的列不为空(在PDO中):

    $sql = 'SELECT * FROM posts where post_date !="" ';
    $stmt = $database->prepare($sql);
    $stmt->execute();
    $rowCount = $stmt->rowCount(); 
    if($rowCount < 1)
    {
        return null;
    }
    else
    {
      do somthing ...
    }