How to count invalid rows in csv file using php?

Isn’t this the same question you asked here ( How to validate mobile number in php? - #7 by droopsnoot ) ? Add an else() clause to your if() statement, to count the ones you reject for the condition:

                if (preg_match('/(7|8|9)\d{9}/', $contact)) {
                    $sql = mysql_query("INSERT INTO csv (contact, message) VALUES ('$contact','$message')");

                 $c = $c + 1;
                }
// ** ADD THIS CODE
                else {
                   $invalid++;
                   }

On the line after you set $c=0, you need another line that sets $invalid =0 in the same format.