0

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?

Whisou138
  • 451
  • 5
  • 21
  • 1
    why you should return `$result` outside the function? – Bhargav Chudasama Sep 07 '18 at 05:08
  • 1
    First things if you are using laravel, why are you using `RAW` sql query ? What is the point in using a framework then. I would suggest you to use eloquent. Here is the official document https://laravel.com/docs/5.6/eloquent on how to use so – user7747472 Sep 07 '18 at 05:19

5 Answers5

1

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;

}
Wreigh
  • 3,215
  • 16
  • 37
  • Thank you, I somehow missed that. So now the page does load again, but still now results. If I copy the query and run it manually with a number instead of the variable though, it returns results – Whisou138 Sep 07 '18 at 13:21
1

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;

    }
Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57
0

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

Raushan Singh
  • 1,070
  • 1
  • 9
  • 18
0

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

WHERE u.ID = '{$id}'

Bernhard
  • 4,855
  • 5
  • 39
  • 70
Defiant_1
  • 16
  • 1
0

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.

Raushan Singh
  • 1,070
  • 1
  • 9
  • 18
increddibelly
  • 1,221
  • 1
  • 15
  • 25