简体   繁体   English

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

[英]Check if php mysql query result column is empty

I'm printing out a table with 27 cols from a database, so its obvious that it'll be aesthetically displeasing if 27 cols were visible on my screen. 我正在从数据库中打印出一个带有27个列的表格,因此很明显,如果在我的屏幕上可以看到27个列,这将在美学上令人不快。 so this is one of the conditions i've been using to see if a particular col is empty, if it is empty then the table header will not be printed and if that isnt printed another if isset condition will not print the table data. 因此,这是我一直在查看特定列是否为空的条件之一,如果该列为空,则将不打印表头,并且如果isset条件不打印则不打印表数据。 But it isn't working out as planned. 但是它没有按计划进行。 These are the variations i've tried and none of them are working PS $result = number of rows being returned by the query. 这些是我尝试过的变体,没有一个起作用。PS $ 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++;
    }   

Your code should be like this: 您的代码应如下所示:

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

Could you use array_key_exists() ? 您可以使用array_key_exists()吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM