php - Laravel 5.1 - 简单的 SQL 查询给出错误

标签 php mysql laravel-5.1

尝试运行一个简单的 SQL 查询,但我不断收到同样的错误,而且找不到解决方案。

查询

    $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');

变量 $user_id$article_id 有效,我通过 echo'ing 测试了它们。

我遇到的错误

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)

我的收藏夹表

1

我该如何解决这个问题? 提前致谢

最佳答案

你应该使用双引号:

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

或单个连接

'select *
from favorites
where user_id = '.$user_id.' AND article_id = '.$article_id ;

关于php - Laravel 5.1 - 简单的 SQL 查询给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33303938/

相关文章:

php - 使用资源 Controller 的存储方法上的 Laravel 5.1 MethodNotAllowedHttpException

Php Laravel 5.1-无法与主机 smtp.gmail.com 建立连接 [Connection refused #111]

php - 对象的array_unique?

javascript - 如果更改了所选值,则运行脚本

php - 返回多个结果的不同 SQL 查询

大量 INSERT 后 MySQL auto_increment 变得疯狂

php - Laravel 5.1 Mail::queue Amazon SQS 错误 404

php - 获取事件菜单-URL

php - 如何验证房间号是否不在数据库中

php - MySQL 未在 Xampp 控制面板上启动