Hello friends,
I have written the following script to count the columns which are not empty.
In the following script i have taken a scenario where student’s batch-days are supposed to be counted.
<?php $sql="SELECT sum((monday_time_from IS NOT NULL and monday_time_from <> '') + (tuesday_time_from IS NOT NULL and tuesday_time_from <> '') + (wednesday_time_from IS NOT NULL and wednesday_time_from <> '') + (thursday_time_from IS NOT NULL and thursday_time_from <> '') + (friday_time_from IS NOT NULL and friday_time_from <> '') + (saturday_time_from IS NOT NULL and saturday_time_from <> '') + (sunday_time_from IS NOT NULL and sunday_time_from <> '')) as total from batches WHERE student_id='$student_id' "; //change monday_time_from,tuesday_time_from,... with your column names $result = mysql_query($sql); $data=mysql_fetch_assoc($result); echo $number_of_days=$data['total']; ?>
871 thoughts on “Count Number of Columns That Are Not Empty:PHP MYSQL”