Error
Error Code:
1691
MySQL Error 1691: Non-Integer in LIMIT Clause
Description
This error occurs when a variable used within the `LIMIT` clause of a MySQL query is not an integer type. MySQL strictly requires integer values for both the offset and count parameters in `LIMIT` to correctly determine the range of rows to retrieve.
Error Message
A variable of a non-integer based type in LIMIT clause
Known Causes
4 known causesUsing String Variables
Attempting to use a string variable, even one containing numeric characters, directly in the `LIMIT` clause will trigger this error.
Decimal or Float Variables
Providing a variable that holds a decimal or floating-point number to the `LIMIT` clause is not permitted, as it requires an exact integer value.
Boolean or Other Non-Numeric Types
Using a boolean, date, or any other non-numeric data type variable in the `LIMIT` clause will result in a type mismatch error.
Implicit Type Conversion Failure
If MySQL attempts an implicit conversion of a variable to an integer for `LIMIT` and the conversion fails, this error can occur.
Solutions
3 solutions available1. Ensure Integer Variables for LIMIT easy
Explicitly cast or ensure variables used in the LIMIT clause are integers.
1
Identify the variable(s) used in your `LIMIT` clause. These are typically integers representing the number of rows to return or the offset.
2
If the variable is coming from an application or a stored procedure, ensure it is declared and treated as an integer type. For example, in PHP, use `(int)` casting. In Python, ensure the variable is an `int`.
SELECT * FROM your_table LIMIT 10 OFFSET (int)$offset_variable;
3
If you are using a user-defined variable in MySQL, declare it as an integer or cast it to an integer before using it in the `LIMIT` clause.
SET @row_count = 5;
SET @offset_val = 10;
SELECT * FROM your_table LIMIT CAST(@row_count AS UNSIGNED), CAST(@offset_val AS UNSIGNED);
2. Direct Integer Literals in LIMIT easy
Replace variables with direct integer values if the dynamic aspect is not strictly necessary.
1
Examine your SQL query. If the `LIMIT` clause is using a variable that can be hardcoded to a specific integer value for this instance, do so.
SELECT * FROM your_table LIMIT 10 OFFSET 20;
2
This is a quick fix for situations where the dynamic nature of the variable is not essential for the immediate query execution. It bypasses the need for variable type checking by the MySQL server.
3. Validate Application Logic for Variable Types medium
Review the code that prepares the SQL query to ensure variables are correctly typed as integers.
1
Locate the part of your application code (e.g., backend script, stored procedure definition) that constructs the SQL query containing the `LIMIT` clause.
2
Inspect the declaration and assignment of variables used in the `LIMIT` clause. Ensure they are explicitly defined as integer types or that any assigned values are convertible to integers.
Example in Python:
python
limit_rows = 5
offset_val = 10
# Ensure they are integers before passing to the query
cursor.execute("SELECT * FROM your_table LIMIT %s OFFSET %s", (int(limit_rows), int(offset_val)))
3
If variables are being derived from user input or external sources, implement robust validation and sanitization to guarantee they are indeed integers before they are used in the `LIMIT` clause.