duan19911992
2018-09-07 04:56
浏览 84
已采纳

将函数参数传递给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条回答 默认 最新

  • doudansui6650 2018-09-07 05:09
    已采纳

    Are you sure the first one is working fine? Because you have a syntax mistake.

    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} 
              ";
    } // <-- remove this, that would end the function block
    // thus causing the error here, unexpected `$result`
         $result = DB::connection('odbc')->select(DB::raw($sql));
          return $result;
    
    }
    
    点赞 评论
  • duanhuanyou6478 2018-09-07 05:02

    why u use syntax like core .use laravel synatx for query if u use second database please define in .env and config>database.php

    use link http://fideloper.com/laravel-multiple-database-connections

    点赞 评论
  • dongshi6969 2018-09-07 05:05

    I am not 100% sure how you are passing $id, but maybe you need the quotes in the SQL query?

    WHERE u.ID = '{$id}'

    点赞 评论
  • dongzi1209 2018-09-07 07:50

    try to this one

    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 = :uid ";
    
              $result = DB::connection('odbc')->select(DB::raw($sql),['uid' => $id]);
              return $result;
    
        }
    
    点赞 评论
  • dongyou9818 2018-09-07 07:55

    Just looking at the number of braces, it doesn't parse. You have two closing braces and one opening brace which means that $result isn't contained in the function. Hence Parse error: syntax error, unexpected '$result'.
    Jignesh Joisar typed the correct method just now.

    点赞 评论

相关推荐 更多相似问题