简体   繁体   中英

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. 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. 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.

$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() ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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