检查php mysql查询结果列是否为空

时间:2012-05-18 18:51:03

标签: php

我正在从数据库中打印出一张包含27列的表格,所以很明显,如果在我的屏幕上可以看到27列,那将是美学上令人不悦的。所以这是我一直用来查看特定col是否为空的条件之一,如果它是空的那么表头将不会被打印,如果没有打印,如果isset条件将不打印表数据。但它没有按计划进行。这些是我尝试过的变化,而且没有一个是P.S. $ result =查询返回的行数。

$i = 1;
while ($i <= $result)

    {
        if (!empty($array['Others'][$i]))
            {

                $others = print "<th>Others</th>";
                break;
            }
        $i++;
    }


$i = 0;
    while ($i <= $result)
    {
        $emptyothers = !empty($array['Others'][$i]);

        if ($emptyothers == '1')
            {

                $others= print "<th>Others</th>";
                break;
            }
        $i++;
    }   

2 个答案:

答案 0 :(得分:5)

您的代码应该是这样的:

$sql = mysql_query("SELECT * FROM table");
if (mysql_num_rows($sql) > 0) {
    //your code...
} else {
    print 'is empty';
}

答案 1 :(得分:1)

你能使用array_key_exists()吗?

foreach($row in $result) {
    if(array_key_exists('Others', $row)) {
        if(!empty($row['Others']) {
            print "<th>Others</th>";
            break;
        }
    }
}
相关问题