1

Trying to run a simple SQL query, but I keep getting the same error and I can't find a solution.

The Query

    $user_id = Auth::user()->id;
    $article_id = Request::input('articleId');
    $results = DB::select('select *
                          from favorites
                          where user_id = $user_id AND article_id = $article_id');

The variables $user_id and $article_id work, I tested that by echo'ing them.

The error I'm getting

SQLSTATE[42S22]: Column not found: 1054 Unknown column '$user_id' in 'where clause' (SQL: select * from favorites where user_id = $user_id AND article_id = $article_id)

my favorites table

1

How can I solve this issue? Thanks in advance

1 Answer 1

3

You should either use double qoutes:

"select *
from favorites
where user_id = $user_id AND article_id = $article_id"

or single with concatenation

'select *
from favorites
where user_id = '.$user_id.' AND article_id = '.$article_id ;
Sign up to request clarification or add additional context in comments.

1 Comment

Or better yet, create a Favourites model and then use Eloquent

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.