Check the LIMIT clause using; it is not possible to use identifiers or variables in LIMIT clause on old MySQL versions, see your SELECT statement which used to insert records.
From the documentation (MySQL 5.1) - The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).
Also: it is strange that the same script works/doesn't works on the same server.