Ehm.. not quite... I think.. 🙂
You do this:
if(preg_match("/$pattern/i", $row[6]))
and you do something depending on it's outcome.
So if it's not behaving like you think it should behave then the problem must be in this line.
So to debug it, I'd suggest printing $row[6] and $pattern to see if it all makes sense int the first place.
It is also much more readable and usable if instead of $row[6], you use $row['fieldname'].
Because, using 6 may work, but if you ever decide to change the table-layout or the query structire, item 6 on the row may change to a different field. When you use the column name, it will allways be the correct column.