duan19911992
duan19911992
2018-09-07 04:56

将函数参数传递给sql查询

  • laravel
  • php
  • sql

I have a weird issue here. I'm passing an ID from a controller into a function file in order to use it in the query. Passing the ID works, and I've dumped it to make sure it's the actual ID I expect and it is.

The problem is, when I add a where clause to my sql, literally only changing the one line, I get the error:

Parse error: syntax error, unexpected '$result' (T_VARIABLE), expecting function (T_FUNCTION)

Here is the working version (though obviously it's a broad query)

public function grabList(int $id)
  {

      $sql = "
        SELECT *
        FROM schemaTest.List L
          INNER JOIN schemaTest.user u
            ON l.user_id = u.ID
          ";
    }

      $result = DB::connection('odbc')->select(DB::raw($sql));
      return $result;

  }

But when I add the where clause that uses the ID passed into the function, I get the error.

public function grabList(int $id)
  {

      $sql = "
        SELECT *
        FROM schemaTest.List L
          INNER JOIN schemaTest.user u
            ON l.user_id = u.ID
        WHERE u.ID = {$id} 
          ";
    }

      $result = DB::connection('odbc')->select(DB::raw($sql));
      return $result;

  }

I'm assuming it's because of the syntax I'm using to try and use the function argument in the query. Any ideas?

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

5条回答

为你推荐

换一换