Column not found

This is not the regular “unknown column error”. As you can see below

“SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘$userEmail’ in ‘where clause’\nThe SQL being executed was: select * from user WHERE email = $userEmail”

as you can see from the error above it is picking the variable $userEmail as a column.

Below is method that has the simple query:

public static function findByLoginDetail($userEmail)


return static::findBySql('select * from user WHERE `email` = $userEmail')->one();


please how do i fix this?

All you need to do is to place $userEmail outside of the quotation marks

return static::findBySql('select * from user WHERE `email` = ' . $userEmail)->one();

Keep in mind you can do that as well, it’s easier to maintain

return static::find()->where(['email' => $userEmail])->limit(1)->one();
1 Like

You had to use double quotes instead of single quotes in order to expand the variable within it.

"select * from user WHERE `email` = $userEmail"

But it’s a dangerous way to construct a SQL. It’s vulnerable to a SQL injection attack.

This is also dangerous.

This is safe.

1 Like