fetchrow_array returns each row as an array. You cannot assign an array to a scalar variable (array in scalar context returns its size). To assign an array reference, you can use
fetchrow_arrayref. By adding the parentheses, you create a list on the left hand side, and the first column is assigned to the variable in the parentheses.
The original code has a bug. You can't get a better result without fixing it.