Counting how many MySQL fields in a row are filled (or empty)

后端 未结 3 1963
被撕碎了的回忆
被撕碎了的回忆 2021-02-09 05:59

I need to put together a method that would allow me to quantify how many fields in a row have been filled by a user.

For example:

User    Name    Age             


        
3条回答
  •  臣服心动
    2021-02-09 06:28

    $result = mysql_query('SELECT * FROM `MyTable`');
    while($row = mysql_fetch_row($result)){
        $empty_count = 0;
        $count = count($row);
        for($i = 0; $i < $count; $i++)
            if($row[$i] === '' || $row[$i] === 'NULL')
                $empty_count++;
        echo 'User '.$row[0].' = '.((int)(100*(1-$empty_count/($count-1)))).'% complete';
    }
    

提交回复
热议问题