Most likely there is a MYSQL error.
1. Is the table really called table_name ?
2. Looks like you are trying to grab the table name from the CGI input into $table, but then you don't do anything with it.
I was grabbing the table name from CGI until the problem started.
Then I just put it in the mysql command line directly. The table is salary_teacher_1999. I was just using table_name as an example since I've got about 50 tables to go through.