【问题标题】:Laravel 5.1 - Simple SQL Query gives errorsLaravel 5.1 - 简单的 SQL 查询给出错误
【发布时间】:2015-10-23 21:50:07
【问题描述】:

尝试运行一个简单的 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]:找不到列:1054 未知列“$user_id”在 'where 子句' (SQL: select * from favorites where user_id = $user_id AND article_id = $article_id)

我最喜欢的表格

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

【问题讨论】:

    标签: php mysql laravel-5.1


    【解决方案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 ;
    

    【讨论】:

    • 或者更好的是,创建一个Favourites 模型,然后使用 Eloquent
    猜你喜欢
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 2019-09-21
    • 2016-01-07
    • 1970-01-01
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多