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?

2
  • 1
    why you should return $result outside the function? Commented Sep 7, 2018 at 5: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 laravel.com/docs/5.6/eloquent on how to use so Commented Sep 7, 2018 at 5:19

5 Answers 5

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;

}
Sign up to request clarification or add additional context in comments.

1 Comment

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
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;

    }

Comments

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

3 Comments

But that doesn't explain why the query works perfectly fine without the one line of the where clause
use have use List L INNER JOIN schemaTest.user u ON l.user_id = u.ID here l is small letter use same as capital L
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}'

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.