Uncaught mysqli_sql_exception: Unknown column in 'where clause'

Uncaught mysqli_sql_exception: Unknown column in 'where clause'

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Uncaught mysqli_sql_exception: Unknown column in 'where clause'

When you encounter Uncaught mysqli_sql_exception: Unknown column 'games' in 'where clause', your first instinct might be to check your database schema or verify that the table exists. However, if a query works perfectly when you filter by a numeric ID but fails with text values like "games," the issue almost certainly lies in how the value is being interpreted within the SQL statement.

The root cause is a collision between your column name and the dynamic data you are trying to match. In your specific scenario, the category column contains a string such as "games". Because you are constructing the query using string concatenation:

$Sql = 'SELECT * FROM `news` WHERE `category` = '.$Param['category'].' ORDER BY `id` DESC';

The database engine receives a statement that looks like this:

SELECT * FROM `news` WHERE `category` = games ORDER BY `id` DESC

When the parser encounters games, it attempts to resolve it as a column name or an alias before realizing it is intended as a literal string value. Since there is no column named games, and assuming no alias exists with that name, the engine throws the "Unknown column" error. This behavior occurs regardless of whether the column type is TINYTEXT, VARCHAR, or any other text storage; SQL parsing logic treats unquoted identifiers as references to columns first.

The Solution: Always Quote String Literals

To resolve this, you must wrap the dynamic value in single quotes when constructing the raw query string. This tells the SQL parser explicitly that "games" is a literal value, not a reference to a table structure.

The corrected concatenation looks like this:

$Sql = 'SELECT * FROM `news` WHERE `category` = \''.$Param['category'].'\' ORDER BY `id` DESC';

By adding the single quotes around $Param['category'], the resulting SQL becomes:

SELECT * FROM `news` WHERE `category` = 'games' ORDER BY `id` DESC

Now, the parser correctly identifies games as a string literal. The confusion between column names and values disappears because the syntax is unambiguous.

Why Numeric Values "Work"

You mentioned that changing the category value to a numeric ID makes the query work. This happens because SQL allows unquoted integers. If you store the value 105 in the database, the parser accepts WHERE category = 105 perfectly fine without quotes. It interprets 105 as an integer literal automatically. The error only manifests when the value contains characters that look like identifiers (letters, underscores, etc.) but are missing the necessary quoting to be treated as strings.

Critical Security Warning: SQL Injection

While fixing the quote issue resolves the immediate crash, your current approach of concatenating user input directly into a query string creates a severe vulnerability known as SQL injection. If a malicious user submits a category value like ' OR '1'='1, they could bypass authentication logic or extract sensitive data from your database.

The robust solution is to use prepared statements with parameter binding. This separates the SQL code structure from the data, ensuring that whatever the user enters is always treated strictly as data, never as executable code.

In pure PHP with mysqli, you would implement it like this:

$stmt = $mysqli->prepare('SELECT * FROM `news` WHERE `category` = ? ORDER BY `id` DESC');
$stmt->bind_param('s', $Param['category']); // 's' indicates a string parameter
$stmt->execute();
$result = $stmt->get_result();

Using prepared statements eliminates the need to manually manage quotes and protects your application from injection attacks. This pattern is standard practice in modern backend development and should be adopted immediately.

If you are building this application using Laravel, the framework handles prepared statements and binding automatically through Eloquent or Query Builders, making it difficult to fall into these traps. For more details on securing database interactions in that ecosystem, see https://laravelcompany.com.

Our network